//
//  DataManager.m
//  leo
//
//  Created by 常艳 on 15/8/26.
//  Copyright (c) 2015年 mdx. All rights reserved.
//由UI层直接使用，负责UI层和数据库之间的交互

#import "DataManager.h"
#import "Task.h"
#import "common.h"
#import "AppDelegate.h"
@implementation DataManager
+(DataManager *)sharedSingleton
{
    static DataManager *dataManager;
    @synchronized(self)
    {
        if(dataManager==NULL)
            dataManager=[[DataManager alloc] init];
    }
    return dataManager;
}
-(id)init
{
    self = [super init];
    self.theHelper=[[DBHelper alloc] init];
    return  self;
}
-(void)dealloc
{
    self.theHelper=NULL;
}
-(BOOL) checkValid
{
    if(self.theHelper==NULL)
    {
        NSLog(@"%@,checkValid,theHelper is null",DATAMANAGER_TAG);
        return NO;
    }
    return YES;
}
/*-----------------------------------------*
   以下接口由UI直接调用
 ------------------------------------------*/
//读取用户信息
-(Manager *)getManagerInfo:(NSString *)accountId deviceUid:(NSString *)deviceUid
{
    NSLog(@"%@,getManagerInfo entered,uid is %@,deviceUid is %@.",DATAMANAGER_TAG,accountId,deviceUid);
    if([self checkValid]==NO)
        return NULL;
    if(accountId==NULL||!([accountId intValue]>0)||deviceUid==NULL||!([deviceUid intValue]>0))
    {
        NSLog(@"%@,getManagerInfo,parameters is illegal.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from manager where deviceuid=%@ and account_id=%@;",deviceUid,accountId];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]<=0)
    {
        NSLog(@"%@,getManagerInfo,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    else
    {
        NSDictionary *managerInfo=[data objectAtIndex:0];
        if(managerInfo==NULL)
        {
            NSLog(@"%@,getManagerInfo,managerInfo is null.",DATAMANAGER_TAG);
            return NULL;
        }
        Manager *manager=[[Manager alloc] init];
        if(manager==NULL)
        {
            NSLog(@"%@,getManagerInfo,manager is null.",DATAMANAGER_TAG);
            return NULL;
        }
        manager.deviceUid=[managerInfo valueForKey:@"deviceuid"];
        manager.user_id=[managerInfo valueForKey:@"user_id"];
        manager.alias=[managerInfo valueForKey:@"alias"];
        manager.pic_url=[managerInfo valueForKey:@"pic_url"];
        manager.birth=[managerInfo valueForKey:@"birth"];
        manager.alias=[managerInfo valueForKey:@"alias"];
        manager.nickName=[managerInfo valueForKey:@"nick_name"];
        manager.type=[Tooler getManagerType:[[managerInfo valueForKey:@"type"] intValue]];
        manager.accountId=[managerInfo valueForKey:@"account_id"];
        manager.groupId=[[managerInfo valueForKey:@"group_id"] intValue];
        manager.isSos=[[managerInfo valueForKey:@"is_sos"] intValue];
        return manager;
    }
    
}
//获取当前登录帐号
-(User *)getActiveUser
{
    NSLog(@"%@,getActiveUser enter.",DATAMANAGER_TAG);
    NSString *nsql=@"select * from account where is_active=1;";
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getActiveUser,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSDictionary *info=[data objectAtIndex:0];
    if(info==NULL||[info count]==0)
    {
        NSLog(@"%@,getActiveUser,info is null.",DATAMANAGER_TAG);
        return NULL;
    }
    User *user=[User sharedSingleton];
    user.uid=[info valueForKey:@"user_id"];
    user.account=[info valueForKey:@"account"];
    user.password=[info valueForKey:@"password"];
    user.type=[[info valueForKey:@"type"] intValue];
    user.is_active=[[info valueForKey:@"is_active"] intValue];
    user.accessToken=[info valueForKey:@"access_token"];
    user.nickName=[info valueForKey:@"nick_name"];
    user.refreshToken=[info valueForKey:@"refresh_token"];
    user.birth=[info valueForKey:@"birth"];
    user.pic_url=[info valueForKey:@"pic_url"];
    user.photoHash=[info valueForKey:@"photohash"];
    return user;
}
//获取管理员列表
-(NSArray *)getManagerLists:(NSString *)deviceUid accountId:(NSString *)accountId
{
    NSLog(@"%@,getManagerLists entered,deviceUid is %@,accountId is %@.",DATAMANAGER_TAG,deviceUid,accountId);
    if([self checkValid]==NO)
        return NULL;
    if(deviceUid==NULL||!([deviceUid intValue]>0)||accountId==NULL||!([accountId intValue]>0))
    {
        NSLog(@"%@,getManagerLists, parameters is illegal.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from manager where deviceuid=%@ and account_id=%@ and (type=%d or type=%d) order by type asc;",deviceUid,accountId,ADMIN,VICE_ADMIN];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getManagerLists,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSMutableArray *managerList=[[NSMutableArray alloc] init];
    if(managerList==NULL)
    {
        NSLog(@"%@,getManagerLists,managerList is null.",DATAMANAGER_TAG);
        return NULL;
    }
    for(int i=0;i<[data count];i++)
    {
        NSDictionary *managerInfo=[data objectAtIndex:i];
        if(managerInfo==NULL||[managerInfo count]==0)
        {
            NSLog(@"%@,getManagerList,managerInfo is null,index is %d.",DATAMANAGER_TAG,i);
            continue;
        }
        Manager *manager=[[Manager alloc] init];
        manager.manager_id=[managerInfo valueForKey:@"id"];
        manager.user_id=[managerInfo valueForKey:@"user_id"];
        manager.alias=[managerInfo valueForKey:@"alias"];
        manager.pic_url=[managerInfo valueForKey:@"pic_url"];
        manager.birth=[managerInfo valueForKey:@"birth"];
        manager.phone=[managerInfo valueForKey:@"phone"];
        manager.type=[Tooler getManagerType:[[managerInfo valueForKey:@"type"] intValue]];
        manager.nickName=[managerInfo valueForKey:@"nick_name"];
        manager.deviceUid=[managerInfo valueForKey:@"deviceuid"];
        manager.accountId=[managerInfo valueForKey:@"account_id"];
        manager.groupId= [[managerInfo valueForKey:@"group_id"] intValue];
        manager.isSos=[[managerInfo valueForKey:@"is_sos"] intValue];
        [managerList addObject:manager];
        manager=NULL;
    }
    return managerList;
}
//获取普通联系人列表
-(NSArray *)getContactLists:(NSString *)deviceUid accountId:(NSString *)accountId
{
    NSLog(@"%@,getContactLists entered,deviceUid is %@,accountId is %@.",DATAMANAGER_TAG,deviceUid,accountId);
    if([self checkValid]==NO)
        return NULL;
    if(deviceUid==NULL||!([deviceUid intValue]>0)||accountId==NULL||!([accountId intValue]>0))
    {
        NSLog(@"%@,getContactLists, parameters is illegal.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from manager where deviceuid=%@ and account_id=%@ and (type=%d or type=%d) group by user_id order by type asc,phone asc;",deviceUid,accountId,ADMIN,VICE_ADMIN];//0106修改
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getContactLists,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSMutableArray *contactList=[[NSMutableArray alloc] init];
    if(contactList==NULL)
    {
        NSLog(@"%@,getContactLists,contactList is null.",DATAMANAGER_TAG);
        return NULL;
    }
    for(int i=0;i<[data count];i++)
    {
        NSDictionary *contactInfo=[data objectAtIndex:i];
        if(contactInfo==NULL||[contactInfo count]==0)
        {
            NSLog(@"%@,getContactLists,contactInfo is null,index is %d.",DATAMANAGER_TAG,i);
            continue;
        }
        /*
        if([contactInfo valueForKey:@"type"]!=NULL&&([[contactInfo valueForKey:@"type"] intValue]==ADMIN||[[contactInfo valueForKey:@"type"] intValue]==VICE_ADMIN))
        {
            Manager *manager=[[Manager alloc] init];
            manager.manager_id=[contactInfo valueForKey:@"id"];
            manager.user_id=[contactInfo valueForKey:@"user_id"];
            manager.alias=[contactInfo valueForKey:@"alias"];
            manager.pic_url=[contactInfo valueForKey:@"pic_url"];
            manager.birth=[contactInfo valueForKey:@"birth"];
            manager.phone=[contactInfo valueForKey:@"phone"];
            manager.type=[Tooler getManagerValue:[[contactInfo valueForKey:@"type"] intValue]];
            manager.nickName=[contactInfo valueForKey:@"nick_name"];
            manager.deviceUid=[contactInfo valueForKey:@"deviceuid"];
            manager.accountId=[contactInfo valueForKey:@"account_id"];
            manager.groupId= [[contactInfo valueForKey:@"group_id"] intValue];
            manager.isSos=[[contactInfo valueForKey:@"is_sos"] intValue];
            [contactList addObject:manager];
            manager=NULL;
        }
        else
        {
            Contact *contact=[[Contact alloc] init];
            contact.phoneNum=[contactInfo valueForKey:@"phone"];
            contact.contactname=[contactInfo valueForKey:@"name"];
            contact.photo_url=[contactInfo valueForKey:@"pic_url"];
            contact.type= [Tooler getContactValue:[[contactInfo valueForKey:@"type"] intValue]];//0106
            contact.user_id=[contactInfo valueForKey:@"user_id"];
            contact.contact_id=[contactInfo valueForKey:@"id"];
            contact.deviceUid=[contactInfo valueForKey:@"deviceuid"];
            contact.accountId=[contactInfo valueForKey:@"account_id"];
            [contactList addObject:contact];
            contact=NULL;
        }
*/
        Manager *manager=[[Manager alloc] init];
        manager.manager_id=[contactInfo valueForKey:@"id"];
        manager.user_id=[contactInfo valueForKey:@"user_id"];
        manager.alias=[contactInfo valueForKey:@"alias"];
        manager.pic_url=[contactInfo valueForKey:@"pic_url"];
        manager.birth=[contactInfo valueForKey:@"birth"];
        manager.phone=[contactInfo valueForKey:@"phone"];
        manager.type=[Tooler getManagerValue:[[contactInfo valueForKey:@"type"] intValue]];
        manager.nickName=[contactInfo valueForKey:@"nick_name"];
        manager.deviceUid=[contactInfo valueForKey:@"deviceuid"];
        manager.accountId=[contactInfo valueForKey:@"account_id"];
        manager.groupId= [[contactInfo valueForKey:@"group_id"] intValue];
        manager.isSos=[[contactInfo valueForKey:@"is_sos"] intValue];
        [contactList addObject:manager];
        manager=NULL;

    }
    
    NSString *nsql1=[NSString stringWithFormat:@"select * from manager where deviceuid=%@ and account_id=%@ and (type=%d or type=%d) group by user_id order by user_id asc;",deviceUid,accountId,CONTACT,SOS_CONTACT];//0106修改
    NSArray *data1=[self.theHelper searchData:[nsql1 UTF8String]];
    if(data1==NULL||[data1 count]==0)
    {
        NSLog(@"%@,getContactLists,data is null.",DATAMANAGER_TAG);
        return contactList;
    }
    for(int i=0;i<[data1 count];i++)
    {
        NSDictionary *contactInfo1=[data1 objectAtIndex:i];
        if(contactInfo1==NULL||[contactInfo1 count]==0)
        {
            NSLog(@"%@,getContactLists,contactInfo1 is null,index is %d.",DATAMANAGER_TAG,i);
            continue;
        }
        Contact *contact1=[[Contact alloc] init];
        contact1.phoneNum=[contactInfo1 valueForKey:@"phone"];
        contact1.contactname=[contactInfo1 valueForKey:@"name"];
        contact1.photo_url=[contactInfo1 valueForKey:@"pic_url"];
        contact1.type= [Tooler getContactValue:[[contactInfo1 valueForKey:@"type"] intValue]];//0106
        contact1.user_id=[contactInfo1 valueForKey:@"user_id"];
        contact1.contact_id=[contactInfo1 valueForKey:@"id"];
        contact1.deviceUid=[contactInfo1 valueForKey:@"deviceuid"];
        contact1.accountId=[contactInfo1 valueForKey:@"account_id"];
        contact1.update_time=[contactInfo1 valueForKey:@"update_time"];
        [contactList addObject:contact1];
        contact1=NULL;
    }

    return contactList;
}
//返回当前选中手表的watch联系人
-(NSArray *)getWatchContacts:(NSString *)deviceUid role:(int)role
{
    if([self checkValid]==NO)
        return NULL;
    if(deviceUid==NULL||!([deviceUid intValue]>0))
    {
        NSLog(@"%@,getWatchContacts, parameters is illegal.",DATAMANAGER_TAG);
        return NULL;
    }
    if(role==VICE_ADMIN)
        return NULL;
    User *user=[User sharedSingleton];
    if(user==NULL||user.uid==NULL)
    {
        NSLog(@"%@,getWatchContacts, user is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from watch where deviceuid<>%@ and account_id=%@ and role=%d;",deviceUid,user.uid,ADMIN];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
        return NULL;
    NSMutableArray *watchList=[[NSMutableArray alloc] init];
    for(int i=0;i<[data count];i++)
    {
        NSDictionary *watchInfo=[data objectAtIndex:i];
        if(watchInfo==NULL)
        {
            NSLog(@"%@,getWatchContacts,watchInfo is null.",watchInfo);
            return NULL;
        }
        Watch *watch=[[Watch alloc ] init];
        watch.deviceUid=[watchInfo valueForKey:@"deviceuid"];
        watch.qrcode_url=[watchInfo valueForKey:@"qrcode_url"];
        watch.sim=[watchInfo valueForKey:@"sim"];
        watch.mac=[watchInfo valueForKey:@"mac"];
        watch.version=[watchInfo valueForKey:@"version"];
        watch.is_latest=[[watchInfo valueForKey:@"is_latest"] intValue];
        watch.nickname=[watchInfo valueForKey:@"nickname"];
        watch.gender=[[watchInfo valueForKey:@"gender"] intValue];
        watch.height=[[watchInfo valueForKey:@"height"] floatValue];
        watch.weight=[[watchInfo valueForKey:@"weight"] floatValue];
        watch.className=[watchInfo valueForKey:@"class"];
        watch.birth=[watchInfo valueForKey:@"birth"];
        watch.pic_url=[watchInfo valueForKey:@"pic_url"];
        watch.update_time=[watchInfo valueForKey:@"update_time"];
        watch.age=[[watchInfo valueForKey:@"age"] intValue];
        watch.imei=[watchInfo valueForKey:@"imei"];
        watch.report_interval=[[watchInfo valueForKey:@"report_interval"] intValue];
        watch.sample_interval=[[watchInfo valueForKey:@"sample_interval"] intValue];
        watch.watchId=[watchInfo valueForKey:@"id"];
        watch.accountId=[watchInfo valueForKey:@"account_id"];
        watch.groupId=[watchInfo valueForKey:@"group_id"];
        watch.battery=[[watchInfo valueForKey:@"battery"] intValue];
        watch.version=[watchInfo valueForKey:@"version"];
        watch.messagePush=[[watchInfo valueForKey:@"message_push"] intValue];
        watch.taskPush=[[watchInfo valueForKey:@"task_push"] intValue];
        watch.role=[[watchInfo valueForKey:@"role"] intValue];
        [watchList addObject:watch];
        watch=NULL;
    }
    return watchList;
}
//获取普通联系人列表，但是不包括手表的主管理员,用于添加SOS联系人时使用
-(NSArray *)getAddSOSContactLists:(NSString *)deviceUid accountId:(NSString *)accountId
{
    NSLog(@"%@,getAddSOSContactLists entered,deviceUid is %@,accountId is %@.",DATAMANAGER_TAG,deviceUid,accountId);
    if([self checkValid]==NO)
        return NULL;
    if(deviceUid==NULL||!([deviceUid intValue]>0)||accountId==NULL||!([accountId intValue]>0))
    {
        NSLog(@"%@,getAddSOSContactLists, parameters is illegal.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from manager where deviceuid=%@ and account_id=%@ and type<>%d group by user_id order by type asc;",deviceUid,accountId,ADMIN];//0106修改
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getAddSOSContactLists,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSMutableArray *sosContactList=[[NSMutableArray alloc] init];
    if(sosContactList==NULL)
    {
        NSLog(@"%@,getAddSOSContactLists,sosContactList is null.",DATAMANAGER_TAG);
        return NULL;
    }
    for(int i=0;i<[data count];i++)
    {
        NSDictionary *contactInfo=[data objectAtIndex:i];
        if(contactInfo==NULL||[contactInfo count]==0)
        {
            NSLog(@"%@,getAddSOSContactLists,contactInfo is null,index is %d.",DATAMANAGER_TAG,i);
            continue;
        }
        if([contactInfo valueForKey:@"type"]!=NULL&&([[contactInfo valueForKey:@"type"] intValue]==ADMIN||[[contactInfo valueForKey:@"type"] intValue]==VICE_ADMIN))
        {
            Manager *manager=[[Manager alloc] init];
            manager.manager_id=[contactInfo valueForKey:@"id"];
            manager.user_id=[contactInfo valueForKey:@"user_id"];
            manager.alias=[contactInfo valueForKey:@"alias"];
            manager.pic_url=[contactInfo valueForKey:@"pic_url"];
            manager.birth=[contactInfo valueForKey:@"birth"];
            manager.phone=[contactInfo valueForKey:@"phone"];
            manager.type=[Tooler getManagerValue:[[contactInfo valueForKey:@"type"] intValue]];
            manager.nickName=[contactInfo valueForKey:@"nick_name"];
            manager.deviceUid=[contactInfo valueForKey:@"deviceuid"];
            manager.accountId=[contactInfo valueForKey:@"account_id"];
            manager.groupId= [[contactInfo valueForKey:@"group_id"] intValue];
            manager.isSos=[[contactInfo valueForKey:@"is_sos"] intValue];
            [sosContactList addObject:manager];
            manager=NULL;
        }
        else
        {
            Contact *contact=[[Contact alloc] init];
            contact.phoneNum=[contactInfo valueForKey:@"phone"];
            contact.contactname=[contactInfo valueForKey:@"name"];
            contact.photo_url=[contactInfo valueForKey:@"pic_url"];
            contact.type=[Tooler getContactValue:[[contactInfo valueForKey:@"type"] intValue]];
            contact.user_id=[contactInfo valueForKey:@"user_id"];
            contact.contact_id=[contactInfo valueForKey:@"id"];
            contact.deviceUid=[contactInfo valueForKey:@"deviceuid"];
            contact.accountId=[contactInfo valueForKey:@"account_id"];
            [sosContactList addObject:contact];
            contact=NULL;
        }
        
    }
    return sosContactList;
}

//获取SOS联系人列表
-(NSArray *)getSOSContactLists:(NSString *)deviceUid accountId:(NSString *)accountId
{
    NSLog(@"%@,getSOSContactLists entered,deviceUid is %@,accountId is %@.",DATAMANAGER_TAG,deviceUid,accountId);
    if([self checkValid]==NO)
        return NULL;
    if(deviceUid==NULL||!([deviceUid intValue]>0)||accountId==NULL||!([accountId intValue]>0))
    {
        NSLog(@"%@,getSOSContactLists, parameters is illegal.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from manager where deviceuid=%@ and account_id=%@ and (type=%d or type=%d or (type=%d and is_sos=%d)) group by user_id order by type asc;",deviceUid,accountId,SOS_CONTACT,ADMIN,VICE_ADMIN,YES];//0106修改
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getSOSContactLists,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSMutableArray *sosContactList=[[NSMutableArray alloc] init];
    if(sosContactList==NULL)
    {
        NSLog(@"%@,getSOSContactLists,sosContactList is null.",DATAMANAGER_TAG);
        return NULL;
    }
    for(int i=0;i<[data count];i++)
    {
        NSDictionary *contactInfo=[data objectAtIndex:i];
        if(contactInfo==NULL||[contactInfo count]==0)
        {
            NSLog(@"%@,getSOSContactLists,contactInfo is null,index is %d.",DATAMANAGER_TAG,i);
            continue;
        }
        if([contactInfo valueForKey:@"type"]!=NULL&&([[contactInfo valueForKey:@"type"] intValue]==ADMIN||[[contactInfo valueForKey:@"type"] intValue]==VICE_ADMIN))
        {
            Manager *manager=[[Manager alloc] init];
            manager.manager_id=[contactInfo valueForKey:@"id"];
            manager.user_id=[contactInfo valueForKey:@"user_id"];
            manager.alias=[contactInfo valueForKey:@"alias"];
            manager.pic_url=[contactInfo valueForKey:@"pic_url"];
            manager.birth=[contactInfo valueForKey:@"birth"];
            manager.phone=[contactInfo valueForKey:@"phone"];
            manager.type=[Tooler getManagerValue:[[contactInfo valueForKey:@"type"] intValue]];
            manager.nickName=[contactInfo valueForKey:@"nick_name"];
            manager.deviceUid=[contactInfo valueForKey:@"deviceuid"];
            manager.accountId=[contactInfo valueForKey:@"account_id"];
            manager.groupId= [[contactInfo valueForKey:@"group_id"] intValue];
            manager.isSos=[[contactInfo valueForKey:@"is_sos"] intValue];
            [sosContactList addObject:manager];
            manager=NULL;
        }
        else
        {
            Contact *contact=[[Contact alloc] init];
            contact.phoneNum=[contactInfo valueForKey:@"phone"];
            contact.contactname=[contactInfo valueForKey:@"name"];
            contact.photo_url=[contactInfo valueForKey:@"pic_url"];
            contact.type=[Tooler getContactValue:[[contactInfo valueForKey:@"type"] intValue]];
            contact.user_id=[contactInfo valueForKey:@"user_id"];
            contact.contact_id=[contactInfo valueForKey:@"id"];
            contact.deviceUid=[contactInfo valueForKey:@"deviceuid"];
            contact.accountId=[contactInfo valueForKey:@"account_id"];
            [sosContactList addObject:contact];
            contact=NULL;
        }

    }
    return sosContactList;
}
//获取管理员详细信息
-(Manager *)getManager:(NSString *)manager_id
{
    NSLog(@"%@,getManager entered,manager_id is %@.",DATAMANAGER_TAG,manager_id);
    if([self checkValid]==NO)
        return NULL;
    if(manager_id==NULL||!([manager_id intValue]>0))
    {
        NSLog(@"%@,getManager,manager_id is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from manager where id=%@;",manager_id];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getManager,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSDictionary *managerInfo=[data objectAtIndex:0];
    if(managerInfo==NULL)
    {
        NSLog(@"%@,getManager,managerInfo is null.",DATAMANAGER_TAG);
        return NULL;
    }
    Manager *manager=[[Manager alloc] init];
    manager.manager_id=[managerInfo valueForKey:@"id"];
    manager.user_id=[managerInfo valueForKey:@"user_id"];
    manager.alias=[managerInfo valueForKey:@"alias"];
    manager.pic_url=[managerInfo valueForKey:@"pic_url"];
    manager.birth=[managerInfo valueForKey:@"birth"];
    manager.phone=[managerInfo valueForKey:@"phone"];
    manager.nickName=[managerInfo valueForKey:@"nick_name"];
    manager.type=[Tooler getManagerType:[[managerInfo valueForKey:@"type"] intValue]];
    manager.isSos=[[managerInfo valueForKey:@"is_sos"] intValue];
    return manager;
    
}
-(Manager *)getManagerByUid:(NSString *)user_id
{
    NSLog(@"%@,getManagerByUid entered,user_id is %@.",DATAMANAGER_TAG,user_id);
    if([self checkValid]==NO)
        return NULL;
    if(user_id==NULL||!([user_id intValue]>0))
    {
        NSLog(@"%@,getManagerByUid,manager_id is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from manager where user_id=%@ limit 1;",user_id];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getManagerByUid,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSDictionary *managerInfo=[data objectAtIndex:0];
    if(managerInfo==NULL)
    {
        NSLog(@"%@,getManagerByUid,managerInfo is null.",DATAMANAGER_TAG);
        return NULL;
    }
    Manager *manager=[[Manager alloc] init];
    manager.manager_id=[managerInfo valueForKey:@"id"];
    manager.user_id=[managerInfo valueForKey:@"user_id"];
    manager.alias=[managerInfo valueForKey:@"alias"];
    manager.pic_url=[managerInfo valueForKey:@"pic_url"];
    manager.birth=[managerInfo valueForKey:@"birth"];
    manager.phone=[managerInfo valueForKey:@"phone"];
    manager.nickName=[managerInfo valueForKey:@"nick_name"];
    manager.type=[Tooler getManagerType:[[managerInfo valueForKey:@"type"] intValue]];
    manager.accountId=[managerInfo valueForKey:@"account_id"];
    manager.groupId=[[managerInfo valueForKey:@"group_id"] intValue];
    manager.isSos=[[managerInfo valueForKey:@"is_sos"] intValue];
    return manager;
    
}

//获取联系人详细信息
-(Contact *)getContact:(NSString *)contact_id
{
    NSLog(@"%@,getContact entered,contact_id is %@.",DATAMANAGER_TAG,contact_id);
    if([self checkValid]==NO)
        return NULL;
    if(contact_id==NULL||!([contact_id intValue]>0))
    {
        NSLog(@"%@,getContact,contact_id is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from manager where id=%@;",contact_id];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getContact,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSDictionary *contactInfo=[data objectAtIndex:0];
    if(contactInfo==NULL||[contactInfo count]==0)
    {
        NSLog(@"%@,getContact,contactInfo is null.",DATAMANAGER_TAG);
        return NULL;
    }
    Contact *contact=[[Contact alloc] init];
    contact.phoneNum=[contactInfo valueForKey:@"phone"];
    contact.contactname=[contactInfo valueForKey:@"name"];
    contact.photo_url=[contactInfo valueForKey:@"pic_url"];
    //contact.type=SOS_CONTACT;
    contact.type=[[contactInfo valueForKey:@"type"]intValue];
    contact.user_id=[contactInfo valueForKey:@"user_id"];
    contact.deviceUid=[contactInfo valueForKey:@"deviceuid"];
    contact.contact_id=[contactInfo valueForKey:@"id"];
    return contact;
}
//获取当前账号绑定的所有手表列表
-(NSArray *)getWatchList:(NSString *)accountId
{
    NSLog(@"%@,getWatchList entered,accountId is %@.",DATAMANAGER_TAG,accountId);
    if([self checkValid]==NO)
        return NULL;
    if(accountId==NULL||!([accountId intValue]>0))
    {
        NSLog(@"%@,getWatchList,accountId is null.",DATAMANAGER_TAG);
        return NULL;
    }
    //NSString *nsql=[NSString stringWithFormat:@"select * from watch where deviceUid in (select deviceUid from manager where account_id=%@);",accountId];
    NSString *nsql=[NSString stringWithFormat:@"select * from watch where  account_id=%@;",accountId];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getWatchList, data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSMutableArray *watchList=[[NSMutableArray alloc] init];
    for(int i=0;i<[data count];i++)
    {
        NSDictionary *watchInfo=[data objectAtIndex:i];
        if(watchInfo==NULL)
        {
            NSLog(@"%@,getWatchList,watchInfo is null.",watchInfo);
            return NULL;
        }
        Watch *watch=[[Watch alloc ] init];
        watch.deviceUid=[watchInfo valueForKey:@"deviceuid"];
        watch.qrcode_url=[watchInfo valueForKey:@"qrcode_url"];
        watch.sim=[watchInfo valueForKey:@"sim"];
        watch.mac=[watchInfo valueForKey:@"mac"];
        watch.version=[watchInfo valueForKey:@"version"];
        watch.is_latest=[[watchInfo valueForKey:@"is_latest"] intValue];
        watch.nickname=[watchInfo valueForKey:@"nickname"];
        watch.gender=[[watchInfo valueForKey:@"gender"] intValue];
        watch.height=[[watchInfo valueForKey:@"height"] floatValue];
        watch.weight=[[watchInfo valueForKey:@"weight"] floatValue];
        watch.className=[watchInfo valueForKey:@"class"];
        watch.birth=[watchInfo valueForKey:@"birth"];
        watch.pic_url=[watchInfo valueForKey:@"pic_url"];
        watch.update_time=[watchInfo valueForKey:@"update_time"];
        watch.age=[[watchInfo valueForKey:@"age"] intValue];
        watch.imei=[watchInfo valueForKey:@"imei"];
        watch.report_interval=[[watchInfo valueForKey:@"report_interval"] intValue];
        watch.sample_interval=[[watchInfo valueForKey:@"sample_interval"] intValue];
        watch.watchId=[watchInfo valueForKey:@"id"];
        watch.accountId=[watchInfo valueForKey:@"account_id"];
        watch.groupId=[watchInfo valueForKey:@"group_id"];
        watch.battery=[[watchInfo valueForKey:@"battery"] intValue];
        watch.version=[watchInfo valueForKey:@"version"];
        watch.messagePush=[[watchInfo valueForKey:@"message_push"] intValue];
        watch.taskPush=[[watchInfo valueForKey:@"task_push"] intValue];
        watch.role=[[watchInfo valueForKey:@"role"] intValue];
        [watchList addObject:watch];
        watch=NULL;
    }
    NSLog(@"%@,getWatchList,count is %d.",DATAMANAGER_TAG,(int)[watchList count]);
    return watchList;
}
-(NSArray *)getGroupWatchList:(int)groupId
{
    NSLog(@"%@,getGroupWatchList entered,groupId is %d.",DATAMANAGER_TAG,groupId);
    if([self checkValid]==NO)
        return NULL;
    if(groupId<=0)
    {
        NSLog(@"%@,getGroupWatchList,groupId is illegal.",DATAMANAGER_TAG);
        return NULL;
    }
    User *user=[User sharedSingleton];
    if(user==NULL)
        return NULL;
    NSString *nsql=[NSString stringWithFormat:@"select * from watch where  group_id=%d and  account_id=%@;",groupId,user.uid];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getGroupWatchList, data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSMutableArray *watchList=[[NSMutableArray alloc] init];
    for(int i=0;i<[data count];i++)
    {
        NSDictionary *watchInfo=[data objectAtIndex:i];
        if(watchInfo==NULL)
        {
            NSLog(@"%@,getGroupWatchList,watchInfo is null.",watchInfo);
            return NULL;
        }
        Watch *watch=[[Watch alloc ] init];
        watch.deviceUid=[watchInfo valueForKey:@"deviceuid"];
        watch.qrcode_url=[watchInfo valueForKey:@"qrcode_url"];
        watch.sim=[watchInfo valueForKey:@"sim"];
        watch.mac=[watchInfo valueForKey:@"mac"];
        watch.version=[watchInfo valueForKey:@"version"];
        watch.is_latest=[[watchInfo valueForKey:@"is_latest"] intValue];
        watch.nickname=[watchInfo valueForKey:@"nickname"];
        watch.gender=[[watchInfo valueForKey:@"gender"] intValue];
        watch.height=[[watchInfo valueForKey:@"height"] floatValue];
        watch.weight=[[watchInfo valueForKey:@"weight"] floatValue];
        watch.className=[watchInfo valueForKey:@"class"];
        watch.birth=[watchInfo valueForKey:@"birth"];
        watch.pic_url=[watchInfo valueForKey:@"pic_url"];
        watch.update_time=[watchInfo valueForKey:@"update_time"];
        watch.age=[[watchInfo valueForKey:@"age"] intValue];
        watch.imei=[watchInfo valueForKey:@"imei"];
        watch.report_interval=[[watchInfo valueForKey:@"report_interval"] intValue];
        watch.sample_interval=[[watchInfo valueForKey:@"sample_interval"] intValue];
        watch.watchId=[watchInfo valueForKey:@"id"];
        watch.accountId=[watchInfo valueForKey:@"account_id"];
        watch.groupId=[watchInfo valueForKey:@"group_id"];
        watch.role=[[watchInfo valueForKey:@"role"] intValue];
        [watchList addObject:watch];
        watch=NULL;
    }
    NSLog(@"%@,getGroupWatchList,count is %d.",DATAMANAGER_TAG,(int)[watchList count]);
    return watchList;

}
//获取指定手表的详细信息
-(Watch *)getWatch:(NSString *)deviceUid
{
    NSLog(@"%@,getWatch entered,deviceUid is %@.",DATAMANAGER_TAG,deviceUid);
    if([self checkValid]==NO)
        return NULL;
    if(deviceUid==NULL||!([deviceUid intValue]>0))
    {
        NSLog(@"%@,getWatch,deviceUid is null.",DATAMANAGER_TAG);
        return NULL;
    }
    User *user=[User sharedSingleton];
    if(user==NULL)
    {
        NSLog(@"%@,getWatch,user is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from watch where deviceuid=%@ and account_id=%@;",deviceUid,user.uid];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getWatch,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSDictionary *watchInfo=[data objectAtIndex:0];
    if(watchInfo==NULL||[watchInfo count]==0)
    {
        NSLog(@"%@,getWatch,watchInfo is null.",DATAMANAGER_TAG);
        return NULL;
    }
    Watch *watch=[[Watch alloc ] init];
    watch.deviceUid=[watchInfo valueForKey:@"deviceuid"];
    watch.qrcode_url=[watchInfo valueForKey:@"qrcode_url"];
    watch.sim=[watchInfo valueForKey:@"sim"];
    watch.mac=[watchInfo valueForKey:@"mac"];
    watch.version=[watchInfo valueForKey:@"version"];
    watch.is_latest=[[watchInfo valueForKey:@"is_latest"] intValue];
    watch.nickname=[watchInfo valueForKey:@"nickname"];
    watch.gender=[[watchInfo valueForKey:@"gender"] intValue];
    watch.height=[[watchInfo valueForKey:@"height"] floatValue];
    watch.weight=[[watchInfo valueForKey:@"weight"] floatValue];
    watch.className=[watchInfo valueForKey:@"class"];
    watch.birth=[watchInfo valueForKey:@"birth"];
    watch.pic_url=[watchInfo valueForKey:@"pic_url"];
    watch.update_time=[watchInfo valueForKey:@"update_time"];
    watch.age=[[watchInfo valueForKey:@"age"] intValue];
    watch.imei=[watchInfo valueForKey:@"imei"];
    watch.report_interval=[[watchInfo valueForKey:@"report_interval"] intValue];
    watch.sample_interval=[[watchInfo valueForKey:@"sample_interval"] intValue];
    watch.watchId=[watchInfo valueForKey:@"id"];
    watch.accountId=[watchInfo valueForKey:@"account_id"];
    watch.groupId=[watchInfo valueForKey:@"group_id"];
    watch.battery=[[watchInfo valueForKey:@"battery"] intValue];
    watch.version=[watchInfo valueForKey:@"version"];
    watch.messagePush=[[watchInfo valueForKey:@"message_push"] intValue];
    watch.taskPush=[[watchInfo valueForKey:@"task_push"] intValue];
    
    //UI更改
    watch.shortNum = [[watchInfo valueForKey:@"shortNum"]intValue];
    return watch;
}
//获取指定手表的安全区域列表
-(NSArray *)getFenceList:(NSString *)deviceUid
{
    NSLog(@"%@,getFenceList entered,deviceUid is %@.",DATAMANAGER_TAG,deviceUid);
    if([self checkValid]==NO)
        return NULL;
    if(deviceUid==NULL||!([deviceUid intValue]>0))
    {
        NSLog(@"%@,getWatch,deviceUid is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from fence where deviceuid=%@;",deviceUid];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getFenceList,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSMutableArray *fenceList=[[NSMutableArray alloc] init];
    if(fenceList==NULL)
    {
        NSLog(@"%@,getFenceList,fenceList is null.",DATAMANAGER_TAG);
        return NULL;
    }
    for(int i=0;i<[data count];i++)
    {
        NSDictionary *fenceInfo=[data objectAtIndex:i];
        if(fenceInfo==NULL||[fenceInfo count]==0)
        {
            NSLog(@"%@,getFenceList,fenceInfo is null.",DATAMANAGER_TAG);
            continue;
        }
        Fence *fence=[[Fence alloc] init];
        fence.fence_id=[fenceInfo valueForKey:@"fence_id"];
        fence.deviceUid=[fenceInfo valueForKey:@"deviceuid"];
        fence.latitude=[[fenceInfo valueForKey:@"latitude"] floatValue];
        fence.longtitude=[[fenceInfo valueForKey:@"longtitude"] floatValue];
        fence.altitude=[[fenceInfo valueForKey:@"altitude"] floatValue];
        fence.desc=[fenceInfo valueForKey:@"description"];
        fence.radius=[[fenceInfo valueForKey:@"radius"] floatValue];
        fence.title=[fenceInfo valueForKey:@"title"];
        fence.start_time=[fenceInfo valueForKey:@"start_time"];
        fence.end_time=[fenceInfo valueForKey:@"end_time"];
        fence.cycle=[fenceInfo valueForKey:@"cycle"] ;
        fence.is_active=[[fenceInfo valueForKey:@"is_active"] intValue];
        fence.type=[[fenceInfo valueForKey:@"type"] intValue];
        [fenceList addObject:fence];
        fence=NULL;
    }
    return fenceList;

}
//获取指定安全区域相信信息
-(Fence *)getFence:(NSString *)fenceId
{
    NSLog(@"%@,getFence entered,fenceId is %@.",DATAMANAGER_TAG,fenceId);
    if([self checkValid]==NO)
        return NULL;
    if(fenceId==NULL||!([fenceId intValue]>0))
    {
        NSLog(@"%@,getFence,fenceId is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from fence where fence_id=%@;",fenceId];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getFence,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSDictionary *fenceInfo=[data objectAtIndex:0];
    if(fenceInfo==NULL||[fenceInfo count]==0)
    {
        NSLog(@"%@,fenceInfo is null.",DATAMANAGER_TAG);
        return NULL;
    }
    Fence *fence=[[Fence alloc] init];
    fence.fence_id=[fenceInfo valueForKey:@"fence_id"];
    fence.deviceUid=[fenceInfo valueForKey:@"deviceuid"];
    fence.latitude=[[fenceInfo valueForKey:@"latitude"] floatValue];
    fence.longtitude=[[fenceInfo valueForKey:@"longtitude"] floatValue];
    fence.altitude=[[fenceInfo valueForKey:@"altitude"] floatValue];
    fence.desc=[fenceInfo valueForKey:@"description"];
    fence.radius=[[fenceInfo valueForKey:@"radius"] floatValue];
    fence.title=[fenceInfo valueForKey:@"title"];
    fence.start_time=[fenceInfo valueForKey:@"start_time"];
    fence.end_time=[fenceInfo valueForKey:@"end_time"];
    fence.cycle=[fenceInfo valueForKey:@"cycle"];
    fence.is_active=[[fenceInfo valueForKey:@"is_active"] intValue];
    fence.type=[[fenceInfo valueForKey:@"type"] intValue];
    return fence;
}
//获取指定手表指定时间段的运动轨迹
-(NSArray *)getLocationList:(NSString *)deviceUid startTime:(NSString *)start endTime:(NSString *)end
{
    NSLog(@"%@,getLocationList entered,deviceUid is %@,start is %@,end is %@.",DATAMANAGER_TAG,deviceUid,start,end);
    if([self checkValid]==NO)
        return NULL;
    if(deviceUid==NULL||!([deviceUid intValue]>0)||start==NULL||end==NULL)
    {
        NSLog(@"%@,getLocationList,parameters is illegal.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from position where deviceuid=%@ and gen_time > %@ and gen_time < %@  order by gen_time asc;",deviceUid,start,end];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getLocationList,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSMutableArray *locationList=[[NSMutableArray alloc] init];
    if(locationList==NULL)
    {
        NSLog(@"%@,getLocationList,locationList is null.",DATAMANAGER_TAG);
        return NULL;
    }
    for(int i=0;i<[data count];i++)
    {
        NSDictionary *locationInfo=[data objectAtIndex:i];
        if(locationInfo==NULL||[locationInfo count]==0)
        {
            NSLog(@"%@,getLocationList,locationInfo is null.",DATAMANAGER_TAG);
            continue;
        }
        Location *location=[[Location alloc] init];
        location.deviceUid=deviceUid;
        location.latitude=[[locationInfo valueForKey:@"latitude"] floatValue];
        location.longtitude=[[locationInfo valueForKey:@"longtitude"] floatValue];
        location.altitude=[[locationInfo valueForKey:@"altitude"] floatValue];
        location.radius=[[locationInfo valueForKey:@"radius"] floatValue];
        location.type=[[locationInfo valueForKey:@"type"] intValue];
        location.updateTime=[locationInfo valueForKey:@"update_time"];
        location.gen_time=[locationInfo valueForKey:@"gen_time"];
        location.locateType=[[locationInfo valueForKey:@"locate_type"] intValue];
        location.mainAddress=[locationInfo valueForKey:@"main_address"];
        location.subAddress=[locationInfo valueForKey:@"sub_address"];
        [locationList addObject:location];
        location=NULL;
    }
    return locationList;

}
//获取指定手表最新位置信息
-(Location *)getLatestLocation:(NSString *)deviceUid
{
    NSLog(@"%@,getLatestLocation entered,deviceUid is %@.",DATAMANAGER_TAG,deviceUid);
    if([self checkValid]==NO)
        return NULL;
    if(deviceUid==NULL||!([deviceUid intValue]>0))
    {
        NSLog(@"%@,getLatestLocation,deviceUid is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from position where deviceuid=%@ order by gen_time desc limit 1;",deviceUid];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getLatestLocation,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSDictionary *locationInfo=[data objectAtIndex:0];
    if(locationInfo==NULL||[locationInfo count]==0)
    {
        NSLog(@"%@,getLatestLocation,locationInfo is null.",DATAMANAGER_TAG);
        return NULL;
    }
    Location *location=[[Location alloc] init];
    location.deviceUid=deviceUid;
    location.latitude=[[locationInfo valueForKey:@"latitude"] floatValue];
    location.longtitude=[[locationInfo valueForKey:@"longtitude"] floatValue];
    location.altitude=[[locationInfo valueForKey:@"altitude"] floatValue];
    location.radius=[[locationInfo valueForKey:@"radius"] floatValue];
    location.type=[[locationInfo valueForKey:@"type"] intValue];
    location.updateTime=[locationInfo valueForKey:@"update_time"];
    location.gen_time=[locationInfo valueForKey:@"gen_time"];
    location.mainAddress=[locationInfo valueForKey:@"main_address"];
    location.subAddress=[locationInfo valueForKey:@"sub_address"];
    location.locateType=[[locationInfo valueForKey:@"locate_type"] intValue];
    return location;
}
//读取家庭圈消息总数
-(int)getAllGroupChatNums:(int)groupId accountId:(NSString *)accountId
{
    if([self checkValid]==NO)
        return NO;
    if(groupId<=0||accountId==NULL||!([accountId intValue]>0))
    {
        NSLog(@"%@,getAllGroupChatNums,groupId is illegal.",DATAMANAGER_TAG);
        return 0;
    }
    NSString *nsql=[NSString stringWithFormat:@"select count(1) as nums from chart where group_id=%d and account_id=%@;",groupId,accountId];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getAllGroupChatNums,data is null.",DATAMANAGER_TAG);
        return 0;
    }
    NSDictionary *info=[data objectAtIndex:0];
    if(info==NULL||[info count]==0)
    {
        NSLog(@"%@,getAllGroupChatNums,info is nulll.",DATAMANAGER_TAG);
        return 0;
    }
    int num=[[info valueForKey:@"nums"] intValue];
    return num;

}
//读取单聊消息总数
-(int)getAllSingleChatNums:(NSString *)parterId myId:(NSString *)myId
{
    if([self checkValid]==NO)
        return NO;
    if(parterId==NULL||!([parterId intValue]>0)||myId==NULL||!([myId intValue]>0))
    {
        NSLog(@"%@,getAllSingleChatNums,parterId or myId is illegal.",DATAMANAGER_TAG);
        return 0;
    }
    NSString *nsql=[NSString stringWithFormat:@"select count(1) as nums from chart where (targetuid=%@ and user_id=%@) or (targetuid=%@ and user_id=%@) and account_id=%@;",myId,parterId,parterId,myId,myId];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getAllSingleChatNums,data is null.",DATAMANAGER_TAG);
        return 0;
    }
    NSDictionary *info=[data objectAtIndex:0];
    if(info==NULL||[info count]==0)
    {
        NSLog(@"%@,getAllSingleChatNums,info is nulll.",DATAMANAGER_TAG);
        return 0;
    }
    int num=[[info valueForKey:@"nums"] intValue];
    return num;
}
//读取家庭圈未读消息数
-(int)getGroupChatNums:(int)groupId accountId:(NSString *)accountId
{
    NSLog(@"%@,getGropuChatNums entered,groupId is %d.",DATAMANAGER_TAG,groupId);
    if([self checkValid]==NO)
        return NO;
    if(groupId<=0||accountId==NULL||!([accountId intValue]>0))
    {
        NSLog(@"%@,getGropuChatNums,groupId is illegal.",DATAMANAGER_TAG);
        return 0;
    }
    NSString *nsql=[NSString stringWithFormat:@"select count(1) as nums from chart where group_id=%d and account_id=%@ and is_view=0 and is_finish=1 and (user_id in (select user_id from manager where group_id=%d and account_id=%@) or user_id in (select deviceuid from watch where  group_id=%d and account_id=%@));",groupId,accountId,groupId,accountId,groupId,accountId];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getGropuChatNums,data is null.",DATAMANAGER_TAG);
        return 0;
    }
    NSDictionary *info=[data objectAtIndex:0];
    if(info==NULL||[info count]==0)
    {
        NSLog(@"%@,getGropuChatNums,info is nulll.",DATAMANAGER_TAG);
        return 0;
    }
    int num=[[info valueForKey:@"nums"] intValue];
    return num;
}
//获取单聊未读消息数
-(int)getSingleChatNums:(NSString *)parterId myId:(NSString *)myId
{
    NSLog(@"%@,getSingleChatNums entered,parterId is %@,myId is %@.",DATAMANAGER_TAG,parterId,myId);
    if([self checkValid]==NO)
        return NO;
    if(parterId==NULL||!([parterId intValue]>0)||myId==NULL||!([myId intValue]>0))
    {
        NSLog(@"%@,getSingleChatNums,parterId or myId is illegal.",DATAMANAGER_TAG);
        return 0;
    }
    NSString *nsql=[NSString stringWithFormat:@"select count(1) as nums from chart where (targetuid=%@ and user_id=%@) and account_id=%@ and is_view=0 and is_finish=1;",myId,parterId,myId];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getSingleChatNums,data is null.",DATAMANAGER_TAG);
        return 0;
    }
    NSDictionary *info=[data objectAtIndex:0];
    if(info==NULL||[info count]==0)
    {
        NSLog(@"%@,getSingleChatNums,info is nulll.",DATAMANAGER_TAG);
        return 0;
    }
    int num=[[info valueForKey:@"nums"] intValue];
    return num;
}
//读取家庭圈所有管理员，排除自己
-(NSArray *)getGroupManagers:(int )groupId accountId:(NSString *)accountId deviceUid:(NSString *)deviceUid
{
    NSLog(@"%@,getGroupManagers entered,groupId is %d,accountId is %@.",DATAMANAGER_TAG,groupId,accountId);
    if([self checkValid]==NO)
        return NULL;
    if(groupId<=0||accountId==NULL||!([accountId intValue]>0))
    {
        NSLog(@"%@,getGroupManagers, parameters is illegal.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from manager where group_id=%d and account_id=%@ and (type=%d or type=%d) and user_id<>%@ and deviceuid=%@ group by user_id;",groupId,accountId,ADMIN,VICE_ADMIN,accountId,deviceUid];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getGroupManagers,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSMutableArray *managerList=[[NSMutableArray alloc] init];
    if(managerList==NULL)
    {
        NSLog(@"%@,getGroupManagers,managerList is null.",DATAMANAGER_TAG);
        return NULL;
    }
    for(int i=0;i<[data count];i++)
    {
        NSDictionary *managerInfo=[data objectAtIndex:i];
        if(managerInfo==NULL||[managerInfo count]==0)
        {
            NSLog(@"%@,getGroupManagers,managerInfo is null,index is %d.",DATAMANAGER_TAG,i);
            continue;
        }
        Manager *manager=[[Manager alloc] init];
        manager.manager_id=[managerInfo valueForKey:@"id"];
        manager.user_id=[managerInfo valueForKey:@"user_id"];
        manager.alias=[managerInfo valueForKey:@"alias"];
        manager.pic_url=[managerInfo valueForKey:@"pic_url"];
        manager.birth=[managerInfo valueForKey:@"birth"];
        manager.phone=[managerInfo valueForKey:@"phone"];
        manager.type=[Tooler getManagerType:[[managerInfo valueForKey:@"type"] intValue]];
        manager.nickName=[managerInfo valueForKey:@"nick_name"];
        manager.isSos=[[managerInfo valueForKey:@"is_sos"] intValue];
        [managerList addObject:manager];
        manager=NULL;
    }
    return managerList;
}
//得到groupId
-(int)getGroupid:(NSString *)deviceUid accountId:(NSString *)accountId
{
    NSLog(@"%@,getGroupid entered,deviceUid is %@,accountId is %@.",DATAMANAGER_TAG,deviceUid,accountId);
    if([self checkValid]==NO)
        return 0;
    if(deviceUid==NULL||!([deviceUid intValue]>0)||accountId==NULL||!([accountId intValue]>0))
    {
        NSLog(@"%@,getGroupid, parameters is illegal.",DATAMANAGER_TAG);
        return 0;
    }
    NSString *nsql=[NSString stringWithFormat:@"select group_id from manager where deviceuid=%@ and account_id=%@ and user_id=%@;",deviceUid,accountId,accountId];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getGroupid,data is null.",DATAMANAGER_TAG);
        return 0;
    }
    NSDictionary *info=[data objectAtIndex:0];
    if(info==NULL||[info count]==0)
    {
        NSLog(@"%@,getGroupid,info is null.",DATAMANAGER_TAG);
        return 0;
    }
    int groupId=[[info valueForKey:@"group_id"] intValue];
    return groupId;
}
-(int)getWatchGroupid:(NSString *)deviceUid accountId:(NSString *)accountId
{
    NSLog(@"%@,getWatchGroupid entered,deviceUid is %@,accountId is %@.",DATAMANAGER_TAG,deviceUid,accountId);
    if([self checkValid]==NO)
    return 0;
    if(deviceUid==NULL||!([deviceUid intValue]>0)||accountId==NULL||!([accountId intValue]>0))
    {
        NSLog(@"%@,getWatchGroupid, parameters is illegal.",DATAMANAGER_TAG);
        return 0;
    }
    NSString *nsql=[NSString stringWithFormat:@"select group_id from watch where deviceuid=%@ and account_id=%@;",deviceUid,accountId];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getWatchGroupid,data is null.",DATAMANAGER_TAG);
        return 0;
    }
    NSDictionary *info=[data objectAtIndex:0];
    if(info==NULL||[info count]==0)
    {
        NSLog(@"%@,getWatchGroupid,info is null.",DATAMANAGER_TAG);
        return 0;
    }
    int groupId=[[info valueForKey:@"group_id"] intValue];
    return groupId;

}
//获取群聊聊天记录
-(NSArray *)getChatLists:(int)groupId accountId:(NSString *)accountId limit:(int)limit
{
    NSLog(@"%@,getChatLists entered,groupId is %d.",DATAMANAGER_TAG,groupId);
    if([self checkValid]==NO)
        return NULL;
    if(groupId<=0)
    {
        NSLog(@"%@,getChatLists,groupId is illegal.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from chart where group_id=%d and account_id=%@ and is_finish=1 and (user_id in (select user_id from manager where group_id=%d and account_id=%@) or user_id in (select deviceuid from watch where  group_id=%d and account_id=%@))order by gen_time desc limit %d;",groupId,accountId,groupId,accountId,groupId,accountId,limit];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getChatLists,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSMutableArray *chatList=[[NSMutableArray alloc] init];
    if(chatList==NULL)
    {
        NSLog(@"%@,getChatLists,chatList is null.",DATAMANAGER_TAG);
        return NULL;
    }
    for (int i=0; i<[data count]; i++) {
        NSDictionary *info=[data objectAtIndex:i];
        if(info==NULL||[info count]==0)
        {
            NSLog(@"%@,getChatLists,info is null.",DATAMANAGER_TAG);
            continue;
        }
        Chat *chat=[[Chat alloc] init];
        chat.user_id=[info valueForKey:@"user_id"];
        chat.file_url=[info valueForKey:@"file_url"];
        chat.duration=[[info valueForKey:@"duration"] intValue];
        chat.genTime=[info valueForKey:@"gen_time"];//转换成日期格式
        chat.groupId=[[info valueForKey:@"group_id"] intValue];
        chat.is_read=[[info valueForKey:@"is_read"] intValue];
        chat.is_send=[[info valueForKey:@"is_send"] intValue];
        chat.chatId=[info valueForKey:@"id"];
        chat.type=[[info valueForKey:@"type"] intValue];
        chat.amr_url=[info valueForKey:@"amr_url"];
        chat.sqeId=[info valueForKey:@"seqid"];
        [chatList addObject:chat];
        chat=NULL;
    }
    return chatList;
}

//获取单聊聊天记录
-(NSArray *)getSingleChatLists:(NSString *)partnerId myId:(NSString *)myId limit:(int)limit
{
    NSLog(@"%@,getSingleChatLists entered,partnerId is %@,myId is %@.",DATAMANAGER_TAG,partnerId,myId);
    if([self checkValid]==NO)
        return NULL;
    if(partnerId==NULL||!([partnerId intValue]>0)||myId==NULL||!([myId intValue]>0))
    {
        NSLog(@"%@,getSingleChatLists,parameters is illegal.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from chart where (( user_id=%@ and  targetuid=%@) or  (user_id=%@ and  targetuid=%@)) and  account_id=%@ and is_finish=1 order by gen_time desc limit %d;",partnerId,myId,myId,partnerId,myId,limit];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getSingleChatLists,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSMutableArray *chatList=[[NSMutableArray alloc] init];
    if(chatList==NULL)
    {
        NSLog(@"%@,getSingleChatLists,chatList is null.",DATAMANAGER_TAG);
        return NULL;
    }
    for (int i=0; i<[data count]; i++) {
        NSDictionary *info=[data objectAtIndex:i];
        if(info==NULL||[info count]==0)
        {
            NSLog(@"%@,getSingleChatLists,info is null.",DATAMANAGER_TAG);
            continue;
        }
        Chat *chat=[[Chat alloc] init];
        chat.user_id=[info valueForKey:@"user_id"];
        chat.file_url=[info valueForKey:@"file_url"];
        chat.duration=[[info valueForKey:@"duration"] intValue];
        chat.genTime=[info valueForKey:@"gen_time"];//转换成日期格式
        chat.is_read=[[info valueForKey:@"is_read"] intValue];
        chat.targetUid=[info valueForKey:@"targetuid"];
        chat.is_send=[[info valueForKey:@"is_send"] intValue];
        chat.chatId=[info valueForKey:@"id"];
        chat.type=[[info valueForKey:@"type"] intValue];
        chat.amr_url=[info valueForKey:@"amr_url"];
        chat.sqeId=[info valueForKey:@"seqid"];
        [chatList addObject:chat];
        chat=NULL;
    }
    return chatList;
}
//把所有群未读消息设置为已读
-(BOOL)setGroupChatRead:(int)groupId accountId:(NSString *)accountId
{
    NSLog(@"%@,setGroupChatRead entered,groupId is %d,accountId is %@.",DATAMANAGER_TAG,groupId,accountId);
    if([self checkValid]==NO)
        return NO;
    if(groupId<=0||accountId==NULL||!([accountId intValue]>0))
    {
        NSLog(@"%@,setGroupChatRead,parameters is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"update chart set is_read=1 where group_id=%d and account_id=%@;",groupId,accountId];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_CHART];
    if(result)
    {
        NSLog(@"%@,setGroupChatRead success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,setGroupChatRead fail.",DATAMANAGER_TAG);
    }
    return result;
}
-(BOOL)setSingleChatRead:(NSString *)partnerId accountId:(NSString *)accountId
{
    NSLog(@"%@,setSingleChatRead entered,partnerId is %@,accountId is %@.",DATAMANAGER_TAG,partnerId,accountId);
    if([self checkValid]==NO)
        return NO;
    if(partnerId==NULL||accountId==NULL)
    {
        NSLog(@"%@,setSingleChatRead,parameters is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"update chart set is_read=1 where (user_id=%@ or targetuid=%@) and account_id=%@;",partnerId,partnerId,accountId];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_CHART];
    if(result)
    {
        NSLog(@"%@,setSingleChatRead success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,setSingleChatRead fail.",DATAMANAGER_TAG);
    }
    return result;
}
//根据user_id得到指定管理员详细信息
-(Manager *)gettheManager:(NSString *)uid accountId:(NSString *)accountId deviceUid:(NSString *)deviceUid
{
    NSLog(@"%@,gettheManager entered,uid is %@.",DATAMANAGER_TAG,uid);
    if([self checkValid]==NO)
        return NULL;
    if(uid==NULL||!([uid intValue]>0)||accountId==NULL||deviceUid==NULL)
    {
        NSLog(@"%@,gettheManager,manager_id is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from manager where user_id=%@  and  account_id=%@ and deviceuid=%@ ;",uid,accountId,deviceUid];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,gettheManager,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSDictionary *managerInfo=[data objectAtIndex:0];
    if(managerInfo==NULL)
    {
        NSLog(@"%@,gettheManager,managerInfo is null.",DATAMANAGER_TAG);
        return NULL;
    }
    Manager *manager=[[Manager alloc] init];
    manager.manager_id=[managerInfo valueForKey:@"id"];
    manager.user_id=[managerInfo valueForKey:@"user_id"];
    manager.alias=[managerInfo valueForKey:@"alias"];
    manager.pic_url=[managerInfo valueForKey:@"pic_url"];
    manager.birth=[managerInfo valueForKey:@"birth"];
    manager.phone=[managerInfo valueForKey:@"phone"];
    manager.type=[Tooler getManagerType:[[managerInfo valueForKey:@"type"] intValue]];
    manager.nickName=[managerInfo valueForKey:@"nick_name"];
    manager.isSos=[[managerInfo valueForKey:@"is_sos"] intValue];
    return manager;
    
}
//获取当前选中的手表
-(Watch *)getActiveWatch:(NSString *)accountId
{
    NSLog(@"%@,getActiveWatch entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NULL;
    if(accountId==NULL||!([accountId intValue]>0))
    {
        NSLog(@"%@,getActiveWatch,accountId is null.",DATAMANAGER_TAG);
        return NULL;
    }
    //NSString *nsql=[NSString stringWithFormat:@"select * from watch where is_active=1 and deviceUid in (select deviceUid from manager where account_id=%@);",accountId];
    NSString *nsql=[NSString stringWithFormat:@"select * from watch where is_active=1 and account_id=%@;",accountId];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    NSArray *data1=NULL;
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getActiveWatch,data is null.",DATAMANAGER_TAG);
       // NSString *nsql1=[NSString stringWithFormat:@"select * from watch where deviceUid in (select deviceUid from manager where account_id=%@) order by update_time desc limit 1;",accountId];
        NSString *nsql1=[NSString stringWithFormat:@"select * from watch where account_id=%@ order by update_time desc limit 1",accountId];
        data1=[self.theHelper searchData:[nsql1 UTF8String]];
        if(data1==NULL||[data1 count]==0)
        {
            NSLog(@"%@,getActiveWatch,data1 is null.",DATAMANAGER_TAG);
            return NULL;
        }
    }
    NSDictionary *watchInfo=NULL;
    if(data!=NULL && [data count]>0)
    {
        watchInfo=[data objectAtIndex:0];
    }
    else
    {
        watchInfo=[data1 objectAtIndex:0];
    }
    if(watchInfo==NULL||[watchInfo count]==0)
    {
        NSLog(@"%@,getActiveWatch,watchInfo is null.",DATAMANAGER_TAG);
        return NULL;
    }
    Watch *watch=[[Watch alloc ] init];
    watch.deviceUid=[watchInfo valueForKey:@"deviceuid"];
    watch.qrcode_url=[watchInfo valueForKey:@"qrcode_url"];
    watch.sim=[watchInfo valueForKey:@"sim"];
    watch.mac=[watchInfo valueForKey:@"mac"];
    watch.version=[watchInfo valueForKey:@"version"];
    watch.is_latest=[[watchInfo valueForKey:@"is_latest"] intValue];
    watch.nickname=[watchInfo valueForKey:@"nickname"];
    watch.gender=[[watchInfo valueForKey:@"gender"] intValue];
    watch.height=[[watchInfo valueForKey:@"height"] floatValue];
    watch.weight=[[watchInfo valueForKey:@"weight"] floatValue];
    watch.className=[watchInfo valueForKey:@"class"];
    watch.birth=[watchInfo valueForKey:@"birth"];
    watch.pic_url=[watchInfo valueForKey:@"pic_url"];
    watch.update_time=[watchInfo valueForKey:@"update_time"];
    watch.age=[[watchInfo valueForKey:@"age"] intValue];
    watch.imei=[watchInfo valueForKey:@"imei"];
    watch.report_interval=[[watchInfo valueForKey:@"report_interval"] intValue];
    watch.sample_interval=[[watchInfo valueForKey:@"sample_interval"] intValue];
    watch.watchId=[watchInfo valueForKey:@"id"];
    watch.accountId=[watchInfo valueForKey:@"account_id"];
    watch.groupId=[watchInfo valueForKey:@"group_id"];
    watch.battery=[[watchInfo valueForKey:@"battery"] intValue];
    watch.messagePush=[[watchInfo valueForKey:@"message_push"] intValue];
    watch.taskPush=[[watchInfo valueForKey:@"task_push"] intValue];
    watch.role=[[watchInfo valueForKey:@"role"] intValue];
    watch.themeId=[watchInfo valueForKey:@"theme_id"];
    watch.watchTheme=[[watchInfo valueForKey:@"watch_theme"] intValue];
    
    //UI更改
    watch.shortNum = [[watchInfo valueForKey:@"shortnum"] intValue];
    return watch;
}

//设置手表状态
-(BOOL)updateWatchState:(NSString *)deviceUid
{
    NSLog(@"%@,updateWatchState entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NO;
    if(deviceUid==NULL||!([deviceUid intValue]>0))
    {
        NSLog(@"%@,updateWatchState, deviceUid is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    User *user=[User sharedSingleton];
    if(user==NULL)
    {
        NSLog(@"%@,updateWatchState,user is null.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql1=[NSString stringWithFormat:@"update watch set is_active=0 and account_id=%@;",user.uid];
    NSString *nsql2=[NSString stringWithFormat:@"update watch set is_active=1 where deviceuid=%@ and account_id=%@;",deviceUid,user.uid];
    NSMutableArray *sqlArray=[[NSMutableArray alloc] init];
    if(sqlArray==NULL)
    {
        NSLog(@"%@,updateWatchState,sqlArray is null.",DATAMANAGER_TAG);
        return NO;
    }
    [sqlArray addObject:nsql1];
    [sqlArray addObject:nsql2];
    if([sqlArray count]==0)
    {
        NSLog(@"%@,updateWatchState,has no sql to exec.",DATAMANAGER_TAG);
        return NO;
    }
    BOOL result=[self.theHelper execSql:sqlArray];
    if(result)
    {
        NSLog(@"%@,updateWatchState success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateWatchState fail.",DATAMANAGER_TAG);
    }
    return result;
}
//获取指定手表的静音时段列表
-(NSArray *)getSilentList:(NSString *)deviceUid
{
    NSLog(@"%@,getSilentList entered,deviceUid is %@.",DATAMANAGER_TAG,deviceUid);
    if([self checkValid]==NO)
        return NULL;
    if(deviceUid==NULL||!([deviceUid intValue]>0))
    {
        NSLog(@"%@,getSilentList,deviceUid is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from event where deviceuid=%@;",deviceUid];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getSilentList,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSMutableArray *silentList=[[NSMutableArray alloc] init];
    if(silentList==NULL)
    {
        NSLog(@"%@,getSilentList,silentList is null.",DATAMANAGER_TAG);
        return NULL;
    }
    for(int i=0;i<[data count];i++)
    {
        NSDictionary *silentInfo=[data objectAtIndex:i];
        if(silentInfo==NULL||[silentInfo count]==0)
        {
            NSLog(@"%@,getSilentList,silentInfo is null.",DATAMANAGER_TAG);
            continue;
        }
        Silent *silent=[[Silent alloc] init];
        silent.slient_id=[silentInfo valueForKey:@"silent_id"];
        silent.title=[silentInfo valueForKey:@"title"];
        silent.startTime=[silentInfo valueForKey:@"start_time"];
        silent.endTime=[silentInfo valueForKey:@"end_time"];
        silent.desc=[silentInfo valueForKey:@"desc"];
        silent.type=[[silentInfo valueForKey:@"type"] intValue];
        silent.active=[[silentInfo valueForKey:@"is_active"] intValue];
        silent.cycle=[silentInfo valueForKey:@"cycle"];
        silent.updateTime=[silentInfo valueForKey:@"update_time"];
        [silentList addObject:silent];
        silent=NULL;
    }
    return silentList;
    
}
//获取指定静音时段
-(Silent *)getSilent:(NSString *)silentId
{
    NSLog(@"%@,getSilent entered,silentId is %@.",DATAMANAGER_TAG,silentId);
    if([self checkValid]==NO)
        return NULL;
    if(silentId==NULL||!([silentId intValue]>0))
    {
        NSLog(@"%@,getSilent,silentId is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from event where silent_id=%@;",silentId];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getSilent,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSDictionary *silentInfo=[data objectAtIndex:0];
    if(silentInfo==NULL||[silentInfo count]==0)
    {
        NSLog(@"%@,getSilent is null.",DATAMANAGER_TAG);
        return NULL;
    }
    Silent *silent=[[Silent alloc] init];
    silent.slient_id=[silentInfo valueForKey:@"silent_id"];
    silent.title=[silentInfo valueForKey:@"title"];
    silent.startTime=[silentInfo valueForKey:@"start_time"];
    silent.endTime=[silentInfo valueForKey:@"end_time"];
    silent.desc=[silentInfo valueForKey:@"desc"];
    silent.type=[[silentInfo valueForKey:@"type"] intValue];
    silent.active=[[silentInfo valueForKey:@"is_active"] intValue];
    silent.cycle=[silentInfo valueForKey:@"cycle"];
    silent.updateTime=[silentInfo valueForKey:@"update_time"];
    return silent;
}
//获取指定手表的提醒列表
-(NSArray *)getReminderList:(NSString *)deviceUid
{
    NSLog(@"%@,getReminderList entered,deviceUid is %@.",DATAMANAGER_TAG,deviceUid);
    if([self checkValid]==NO)
        return NULL;
    if(deviceUid==NULL||!([deviceUid intValue]>0))
    {
        NSLog(@"%@,getReminderList,deviceUid is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from remind where deviceuid=%@;",deviceUid];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getReminderList,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSMutableArray *remindList=[[NSMutableArray alloc] init];
    if(remindList==NULL)
    {
        NSLog(@"%@,getReminderList,remindList is null.",DATAMANAGER_TAG);
        return NULL;
    }
    for(int i=0;i<[data count];i++)
    {
        NSDictionary *remindInfo=[data objectAtIndex:i];
        if(remindInfo==NULL||[remindInfo count]==0)
        {
            NSLog(@"%@,getReminderList,remindInfo is null.",DATAMANAGER_TAG);
            continue;
        }
        Reminder *reminder=[[Reminder alloc] init];
        reminder.reminder_id=[remindInfo valueForKey:@"remind_id"];
        reminder.is_active=[[remindInfo valueForKey:@"is_active"] intValue];
        reminder.cycle=[remindInfo valueForKey:@"cycle"];
        reminder.audioHash=[remindInfo valueForKey:@"audio_hash"];
        reminder.remindTime=[remindInfo valueForKey:@"remind_time"];
        reminder.title=[remindInfo valueForKey:@"title"];
        reminder.desc=[remindInfo valueForKey:@"desc"];
        reminder.type=[[remindInfo valueForKey:@"type"] intValue];
        reminder.updateTime=[remindInfo valueForKey:@"update_time"];
        reminder.audioUrl=[remindInfo valueForKey:@"audio_url"];
        reminder.autionTime=[[remindInfo valueForKey:@"audio_time"] intValue];
        [remindList addObject:reminder];
        reminder=NULL;
    }
    return remindList;
    
}
//获取指定提醒
-(Reminder *)getReminder:(NSString *)reminderId
{
    NSLog(@"%@,getReminder entered,reminderId is %@.",DATAMANAGER_TAG,reminderId);
    if([self checkValid]==NO)
        return NULL;
    if(reminderId==NULL||!([reminderId intValue]>0))
    {
        NSLog(@"%@,getReminder,reminderId is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from remind where remind_id=%@;",reminderId];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getReminder,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSDictionary *remindInfo=[data objectAtIndex:0];
    if(remindInfo==NULL||[remindInfo count]==0)
    {
        NSLog(@"%@,getReminder is null.",DATAMANAGER_TAG);
        return NULL;
    }
    Reminder *reminder=[[Reminder alloc] init];
    reminder.reminder_id=[remindInfo valueForKey:@"remind_id"];
    reminder.is_active=[[remindInfo valueForKey:@"is_active"] intValue];
    reminder.cycle=[remindInfo valueForKey:@"cycle"];
    reminder.audioHash=[remindInfo valueForKey:@"audio_hash"];
    reminder.remindTime=[remindInfo valueForKey:@"remind_time"];
    reminder.title=[remindInfo valueForKey:@"title"];
    reminder.desc=[remindInfo valueForKey:@"desc"];
    reminder.type=[[remindInfo valueForKey:@"type"] intValue];
    reminder.updateTime=[remindInfo valueForKey:@"update_time"];
    reminder.audioUrl=[remindInfo valueForKey:@"audio_url"];
    reminder.autionTime=[[remindInfo valueForKey:@"audio_time"] intValue];
    return reminder;
}
-(NSArray *)getMessageList:(NSString *)accountId deviceUid:(NSString *)deviceUid
{
    NSLog(@"%@,getMessageList entered,accountId is %@.",DATAMANAGER_TAG,accountId);
    if([self checkValid]==NO)
        return NULL;
    if(accountId==NULL||!([accountId intValue]>0)||deviceUid==NULL)
    {
        NSLog(@"%@,getMessageList,accountId is null.",DATAMANAGER_TAG);
        return NULL;
    }
    //NSString *nsql=[NSString stringWithFormat:@"select message.*,position.* from message   left join position on message.loc_id=position.id and message.account_id=%@ and message.deviceUid=%@  order by message.create_time desc;",accountId,deviceUid];
    NSString *nsql=[NSString stringWithFormat:@"select a.*,position.* from (select message.*,message.id as mid,message.deviceuid as duid from message  where message.account_id=%@ and message.deviceuid=%@ and message.is_delete=0)  as a left join position on a.loc_id=position.id  order by position.gen_time desc, a.create_time desc;",accountId,deviceUid];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]<=0)
    {
        NSLog(@"%@,getMessageList,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSMutableArray *messageList=[[NSMutableArray alloc] init];
    if(messageList==NULL)
    {
        NSLog(@"%@,getMessageList,messageList is null.",DATAMANAGER_TAG);
        return NULL;
    }
    for(int i=0;i<[data count];i++)
    {
        NSDictionary *messageInfo=[data objectAtIndex:i];
        if(messageInfo==NULL||[messageInfo count]<=0)
        {
            NSLog(@"%@,getMessageList,messageInfo is null,index is %d.",DATAMANAGER_TAG,i);
            continue;
        }
        PMessage *message=[[PMessage alloc] init];
        message.deviceUid=[messageInfo valueForKey:@"duid"];
        message.title=[messageInfo valueForKey:@"title"];
        message.uid=[messageInfo valueForKey:@"uid"];
        message.nickName=[messageInfo valueForKey:@"nick_name"];
        message.commentValue=[[messageInfo valueForKey:@"value"] intValue];
        message.alias=[messageInfo valueForKey:@"alias"];
        message.genTime=[messageInfo valueForKey:@"create_time"];
        message.fid=[messageInfo valueForKey:@"fence_id"];
        message.locId=[[messageInfo valueForKey:@"loc_id"] intValue];
        message.fenceType=[[messageInfo valueForKey:@"fen_type"] intValue];
        message.accountId=[messageInfo valueForKey:@"account_id"];
        message.battery=[[messageInfo valueForKey:@"battery"] intValue];
        message.type=[[messageInfo valueForKey:@"msg_type"] intValue];
        message.specialTime=[messageInfo valueForKey:@"special_time"];
        message.specialName=[messageInfo valueForKey:@"special_name"];
        message.randomCode=[messageInfo valueForKey:@"random_code"];
        message.msgId=[messageInfo valueForKey:@"mid"];
        Location *location=[[Location alloc] init];
        location.radius=[[messageInfo valueForKey:@"radius"] floatValue];
        location.latitude=[[messageInfo valueForKey:@"latitude"] floatValue];
        location.longtitude=[[messageInfo valueForKey:@"longtitude"] floatValue];
        location.type=[[messageInfo valueForKey:@"type"] intValue];
        location.locateType=[[messageInfo valueForKey:@"locate_type"] intValue];
        location.loc=[messageInfo valueForKey:@"loc"];
        location.gen_time=[messageInfo valueForKey:@"gen_time"];
        message.location=location;
        location=NULL;
        [messageList addObject:message];
        message=NULL;
        
    }
    return messageList;
   
}
-(BOOL)updateBindConfirm:(NSString *)msgId confirmValue:(int)confirmValue
{
    NSLog(@"%@,updateBindConfirm entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NO;
    if(msgId==NULL||!([msgId intValue]>0))
    {
        NSLog(@"%@,updateBindConfirm,msgId is null.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=NULL;
    if(confirmValue==1)//disagree
    {
        nsql=[NSString stringWithFormat:@"update message set value=%d,create_time=\"%@\"  where id=%@;",confirmValue,[Tooler getTS],msgId];
    }
    else
    {
        nsql=[NSString stringWithFormat:@"update message set value=%d,create_time=\"%@\"  ,is_delete=1 where id=%@;",confirmValue,[Tooler getTS],msgId];
    }
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_MESSAGE];
    if(result)
    {
        NSLog(@"%@,updateBindConfirm success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateBindConfirm fail.",DATAMANAGER_TAG);
    }
    return result;
}
/*-----------------------------------------*
以下接口由SyncManager直接调用，UI直接忽略
 ------------------------------------------*/
//把用户信息写入数据库
-(BOOL)registerUser:(User *)user
{
    NSLog(@"%@,registerUser entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NO;
    if(user==NULL||user.uid==NULL||!(user.uid>0)||user.account==NULL||user.password==NULL)
    {
        NSLog(@"%@,registerUser,user is null",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"insert into account (user_id,account,password,is_active,access_token,refresh_token,create_time,type,nick_name) values (%@,\"%@\",\"%@\",%d,\"%@\",\"%@\",\"%@\",%d,\"%@\")",user.uid,user.account,user.password,user.is_active,user.accessToken,user.refreshToken,user.createTime,user.type,user.nickName];
    const char *sql=[nsql UTF8String];
    BOOL result=[self.theHelper insertData:sql tableName:TABLE_USER];
    return result;
}
-(BOOL)updateUser:(User *)user
{
    NSLog(@"%@,updateUser entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NO;
    if(user==NULL||user.uid==NULL||!(user.uid>0)||user.account==NULL||user.password==NULL)
    {
        NSLog(@"%@,updateUser,user is null",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql1=[NSString stringWithFormat:@"select * from account where user_id=%@",user.uid];
    const char *sql1=[nsql1 UTF8String];
    NSArray *data1=[self.theHelper searchData:sql1];
    BOOL result=NO;
    if(data1!=NULL&&[data1 count]>0)//用户存在，更新用户信息
    {
        NSString *nsql=[NSString stringWithFormat:@"update account set account=\"%@\",password=\"%@\",is_active=%d,access_token=\"%@\",refresh_token=\"%@\",nick_name=\"%@\",pic_url=\"%@\",photohash=\"%@\",birth=\"%@\"  where user_id=%@",user.account,user.password,user.is_active,user.accessToken,user.refreshToken,user.nickName,user.photoName,user.photoHash,user.birth,user.uid];
         const char *sql=[nsql UTF8String];
        result=[self.theHelper updateData:sql tableName:TABLE_USER];
    }
    else
    {
         NSString *nsql=[NSString stringWithFormat:@"insert into account (user_id,account,password,is_active,access_token,refresh_token,create_time,type,nick_name,pic_url) values (%@,\"%@\",\"%@\",%d,\"%@\",\"%@\",\"%@\",%d,\"%@\",\"%@\")",user.uid,user.account,user.password,user.is_active,user.accessToken,user.refreshToken,user.createTime,user.type,user.nickName,user.photoName];
         const char *sql=[nsql UTF8String];
        result=[self.theHelper updateData:sql tableName:TABLE_USER];
    }
    if(result)
    {
        NSLog(@"%@,updateUser success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateUser fail.",DATAMANAGER_TAG);

    }
  
    return result;
}
-(BOOL)updateUserInfo:(User *)user
{
    NSLog(@"%@,updateUserInfo entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NO;
    if(user==NULL||user.uid==NULL||!(user.uid>0)||user.account==NULL||user.password==NULL)
    {
        NSLog(@"%@,updateUserInfo,user is null",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql1=[NSString stringWithFormat:@"select * from account where user_id=%@;",user.uid];
    const char *sql1=[nsql1 UTF8String];
    NSArray *data1=[self.theHelper searchData:sql1];
    BOOL result=NO;
    if(data1!=NULL&&[data1 count]>0)//用户存在，更新用户信息
    {
        NSString *nsql=[NSString stringWithFormat:@"update account set account=\"%@\",password=\"%@\",is_active=%d,access_token=\"%@\",refresh_token=\"%@\"   where user_id=%@;",user.account,user.password,user.is_active,user.accessToken,user.refreshToken,user.uid];
        const char *sql=[nsql UTF8String];
        result=[self.theHelper updateData:sql tableName:TABLE_USER];
    }
    else
    {
        NSString *nsql=[NSString stringWithFormat:@"insert into account (user_id,account,password,is_active,access_token,refresh_token,create_time,type,nick_name,pic_url) values (%@,\"%@\",\"%@\",%d,\"%@\",\"%@\",\"%@\",%d,\"%@\",\"%@\");",user.uid,user.account,user.password,user.is_active,user.accessToken,user.refreshToken,user.createTime,user.type,user.nickName,user.photoName];
        const char *sql=[nsql UTF8String];
        result=[self.theHelper updateData:sql tableName:TABLE_USER];
    }
    if(result)
    {
        NSLog(@"%@,updateUserInfo success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateUserInfo fail.",DATAMANAGER_TAG);
        
    }
    
    return result;
}

-(BOOL)resetPwd:(NSString *)phoneNum password:(NSString *)password
{
     NSLog(@"%@,resetPwd entered. phoneNum is %@, password is %@.",DATAMANAGER_TAG,phoneNum,password);
    if(phoneNum==NULL||password==NULL)
    {
        NSLog(@"%@,resetPwd,parameters are illegal",DATAMANAGER_TAG);
        return NO;
    }
    if([self checkValid]==NO)
        return NO;
    NSString *nsql=[NSString stringWithFormat:@"update account set password=\"%@\" where account=\"%@\";",password,phoneNum];
    const char *sql=[nsql UTF8String];
    BOOL result=[self.theHelper updateData:sql tableName:TABLE_USER];
    if(result)
    {
        NSLog(@"%@,resetPwd success",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,resetPwd fail",DATAMANAGER_TAG);
    }
    return result;
}
-(BOOL)addWatch:(Watch *)watch
{
    NSLog(@"%@,addWatch entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NO;
    if(watch==NULL||watch.deviceUid==NULL||!([watch.deviceUid integerValue]>0))
    {
        NSLog(@"%@,addWatch,watch is null",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql1=[NSString stringWithFormat:@"select * from watch where deviceuid=%@ and account_id=%@;",watch.deviceUid,watch.accountId];
    const char *sql1=[nsql1 UTF8String];
    NSArray *data1=[self.theHelper searchData:sql1];
    NSString *nsql;
    if(data1!=NULL&&[data1 count]>0)//watch信息已存在
    {
        NSLog(@"%@,addWatch,watch exist.",DATAMANAGER_TAG);
        nsql=[NSString stringWithFormat:@"update watch set nickname=\"%@\",gender=%d,height=%f,weight=%f,birth=\"%@\",class=\"%@\",pic_url=\"%@\",is_active=%d,group_id=%@,sim=\"%@\",role=%d where deviceuid=%@ and account_id=%@; ",watch.nickname,watch.gender,watch.height,watch.weight,watch.birth,watch.className,watch.saveUrl,watch.is_active,watch.groupId,watch.sim,watch.role,watch.deviceUid,watch.accountId];
    }
    else
    {
        nsql=[NSString stringWithFormat:@"insert into watch (deviceuid,qrcode_url,sim,mac,version,is_latest,nickname,gender,height,weight,birth,class,pic_url,update_time,is_active,account_id,group_id,role) values (%@,\"%@\",\"%@\",\"%@\",\"%@\",%d,\"%@\",%d,%f,%f,\"%@\",\"%@\",\"%@\",\"%@\",%d,%@,%@,%d)",watch.deviceUid,watch.qrcode_url,watch.sim,watch.mac,watch.version,watch.is_latest,watch.nickname,watch.gender,watch.height,watch.weight,watch.birth,watch.className,watch.saveUrl,watch.update_time,watch.is_active,watch.accountId,watch.groupId,watch.role];
    }
    const char *sql=[nsql UTF8String];
    BOOL result=[self.theHelper insertData:sql tableName:TABLE_WATCH];
    NSString *nsql2=[NSString stringWithFormat:@"update watch set is_active=0 where deviceuid<>%@ and account_id=%@;",watch.deviceUid,watch.accountId];
    BOOL result2=[self.theHelper updateData:[nsql2 UTF8String] tableName:TABLE_WATCH];
        if(result&&result2)
        {
            NSLog(@"%@,addWatch success",DATAMANAGER_TAG);
        }
        else
        {
            NSLog(@"%@,addWatch fail",DATAMANAGER_TAG);
        }

    
        return result;
    

}
-(BOOL)updateWatchInfo:(Watch *)watch
{
    NSLog(@"%@,updateWatchInfo entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NO;
    if(watch==NULL||!([watch.deviceUid intValue]>0))
    {
        NSLog(@"%@,updateWatchInfo,watch is null",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"update watch set version=\"%@\",battery=%d where deviceuid=%@;",watch.version,watch.battery,watch.deviceUid];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_WATCH];
    if(result)
    {
        NSLog(@"%@,updateWatchInfo success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateWatchInfo fail.",DATAMANAGER_TAG);
    }
    return result;
}
-(BOOL)addManager:(Manager *)manager
{
    NSLog(@"%@,addManager entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NO;
    if(manager==NULL||manager.user_id==NULL||!([manager.user_id intValue]>0)||manager.accountId==NULL||!([manager.accountId intValue]>0))
    {
        NSLog(@"%@,addManager, parameters are illegal.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql1=[NSString stringWithFormat:@"select * from manager where user_id=%@ and deviceuid=%@ and account_id=%@;",manager.user_id,manager.deviceUid,manager.accountId];
    NSArray *data1=[self.theHelper searchData:[nsql1 UTF8String]];
    BOOL result=NO;
    if(data1!=NULL&&[data1 count]>0)
    {
        //更新该管理员所有数据
        NSString *nsql2=[NSString stringWithFormat:@"update manager set alias=\"%@\",phone=\"%@\",nick_name=\"%@\",group_id=%d where user_id=%@ and deviceuid=%@ and account_id=%@;",manager.alias,manager.phone,manager.nickName,manager.groupId,manager.user_id,manager.deviceUid,manager.accountId];
        result=[self.theHelper updateData:[nsql2 UTF8String] tableName:TABLE_MANAGER];
    }
    else
    {
        NSString *nsql2=[NSString stringWithFormat:@"insert into manager(user_id,alias,pic_url,birth,phone,name,update_time,deviceuid,type,account_id,group_id,nick_name)  values (%@,\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",%@,%d,%@,%d,\"%@\");",manager.user_id,manager.alias,manager.pic_url,manager.birth,manager.phone,manager.name,manager.update_time,manager.deviceUid,manager.type,manager.accountId,manager.groupId,manager.nickName];
        result=[self.theHelper insertData:[nsql2 UTF8String] tableName:TABLE_MANAGER];
    }
    if(result)
    {
        NSLog(@"%@,addManager success",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,addManager fail",DATAMANAGER_TAG);
    }
    return result;
}
-(BOOL)deleteManager:(Manager *)manager
{
    NSLog(@"%@,deleteManager entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NO;
    if(manager==NULL||manager.user_id==NULL||!([manager.user_id intValue]>0))
    {
        NSLog(@"%@,deleteManager, managerUid is illegal.",DATAMANAGER_TAG);
        return NO;
    }
        //没有其他绑定手表，可以删除,群组策略修改1230
        NSString *nsql=[NSString stringWithFormat:@"delete from manager where user_id=%@ and group_id=%d and account_id=%@;",manager.user_id,manager.groupId,manager.accountId];
        const char *sql=[nsql UTF8String];
        BOOL result=[self.theHelper deleteData:sql tableName:TABLE_MANAGER];
        if(result)
        {
            NSLog(@"%@,deleteManager success",DATAMANAGER_TAG);
        }
        else
        {
            NSLog(@"%@,deleteManager fail",DATAMANAGER_TAG);
        }
        return result;
    

}

-(BOOL)updateWatches:(NSArray *)watchLists
{
    NSLog(@"%@,updateWatches entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NO;
    if(watchLists==NULL||[watchLists count]==0 )
    {
        NSLog(@"%@,updateWatches,watchLists is null.",DATAMANAGER_TAG);
        return NO;
    }
    NSMutableArray *sqlArray=[[NSMutableArray alloc] init];
    if(sqlArray==NULL)
    {
        NSLog(@"%@,updateWatches,sqlArray is null.",DATAMANAGER_TAG);
        return NO;
    }
    [sqlArray addObject:@"PRAGMA foreign_keys = ON;"];
    for(int i=0;i<[watchLists count];i++)
    {
        NSString *nsql=NULL;
        Watch *watch=[watchLists objectAtIndex:i];
        if(watch.deviceUid==NULL)
        {
            NSLog(@"%@,updateWatches,deviceUid is null, index is %d",DATAMANAGER_TAG,i);
            continue;
        }
        if(watch.action!=NULL&&[watch.action intValue]==0)//手表已和用户解除绑定
        {
            nsql=[NSString stringWithFormat:@"delete from watch where deviceuid=%@ and account_id=%@;",watch.deviceUid,watch.accountId];
            [self clearWatchData:watch.deviceUid accountId:watch.accountId];
        }
        else
        {
            //先判断手表是否存在
            NSString *nsql1=[NSString stringWithFormat:@"select * from watch where deviceuid=%@ and account_id=%@",watch.deviceUid,watch.accountId];
            const char *sql1=[nsql1 UTF8String];
            NSArray *data=[self.theHelper searchData:sql1];
            if(data!=NULL&&[data count]>0)//手表已存在，更新
            {

               nsql=[NSString stringWithFormat:@"update watch set sim=\"%@\" ,nickname=\"%@\",gender=%d,birth=\"%@\",weight=%f,height=%f,age=%d,mac=\"%@\",imei=\"%@\"  ,update_time=\"%@\",class=\"%@\",role=%d,group_id=%@ where deviceuid=\"%@\" and account_id=%@; ",watch.sim,watch.nickname,watch.gender,watch.birth,watch.weight,watch.height,watch.age,watch.mac,watch.imei,watch.update_time,watch.className,watch.role,watch.groupId,watch.deviceUid,watch.accountId];
            }
            else//手表不存在，插入
            {
               nsql=[NSString stringWithFormat:@"insert into watch (deviceuid,qrcode_url,sim,mac,version,is_latest,nickname,gender,height,weight,birth,class,update_time,imei,class,account_id,group_id,is_active,role) values (%@,\"%@\",\"%@\",\"%@\",\"%@\",%d,\"%@\",%d,%f,%f,\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",%@,%@,1,%d)",watch.deviceUid,watch.qrcode_url,watch.sim,watch.mac,watch.version,watch.is_latest,watch.nickname,watch.gender,watch.height,watch.weight,watch.birth,watch.className,watch.update_time,watch.imei,watch.className,watch.accountId,watch.groupId,watch.role];            }

        }
        [sqlArray addObject:nsql];
        nsql=NULL;
        
    }
    
    if(sqlArray==NULL||[sqlArray count]==0)
    {
        NSLog(@"%@,updateWatches,sqlArray is null or count is 0",DATAMANAGER_TAG);
        return NO;
    }
    BOOL result=[self.theHelper execSql:sqlArray];
    if(result)
    {
        NSLog(@"%@,updateWatches success",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateWatches fail",DATAMANAGER_TAG);
    }
    return result;
}
-(BOOL)updateManagers:(NSArray *)managerLists
{
    NSLog(@"%@,updateManagers entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NO;
    if(managerLists==NULL||[managerLists count]==0 )
    {
        NSLog(@"%@,updateManagers,managerLists is null.",DATAMANAGER_TAG);
        return NO;
    }
    NSMutableArray *sqlArray=[[NSMutableArray alloc] init];
    if(sqlArray==NULL)
    {
        NSLog(@"%@,updateManagers,sqlArray is null.",DATAMANAGER_TAG);
        return NO;
    }
    [sqlArray addObject:@"PRAGMA foreign_keys = ON;"];
    for(int i=0;i<[managerLists count];i++)
    {
        NSString *nsql=NULL;
        Manager *manager=[managerLists objectAtIndex:i];
        if(manager.user_id==NULL)
        {
            NSLog(@"%@,updateManagers,user_id is null, index is %d",DATAMANAGER_TAG,i);
            continue;
        }
        // 判断管理人员是否存在
        NSString *nsql1=[NSString stringWithFormat:@"select * from manager where user_id=%@ and deviceuid=%@ and account_id=%@",manager.user_id,manager.deviceUid,manager.accountId];
        const char *sql1=[nsql1 UTF8String];
        NSArray *data=[self.theHelper searchData:sql1];
        if(data!=NULL&&[data count]>0)//管理员存在
        {
            NSLog(@"%@,updateManagers,uid is %@, alreay exist,action is %@.",DATAMANAGER_TAG,manager.user_id,manager.action);
            if(manager.action!=NULL&& [manager.action intValue]==0)//已解绑
            {
                nsql=[NSString stringWithFormat:@"delete from manager where user_id=%@ and deviceuid=%@ and account_id=%@;",manager.user_id,manager.deviceUid,manager.accountId];
                //删除副管理员聊天消息
                //NSString *nsql2=[NSString stringWithFormat:@"delete from chart where user_id=%@ and group_id=%d and account_id=%@;",manager.user_id,manager.groupId,manager.accountId];
                //[sqlArray addObject:nsql2];//群聊消息不删除
                /*
                NSString *nsql3=[NSString stringWithFormat:@"select distinct group_id from manager where user_id=%@ and account_id=%@;",manager.user_id,manager.accountId];
                NSArray *data3=[self.theHelper searchData:[nsql3 UTF8String]];
                if(data3!=NULL&&[data3 count]>1)
                {
                    
                }
                else
                {
                    NSString *nsql4=[NSString stringWithFormat:@"delete from chart where (user_id=%@ or  targetuid=%@ ) and account_id=%@;",manager.user_id,manager.user_id,manager.accountId];
                    [sqlArray addObject:nsql4];
                }*/
                NSMutableDictionary *viceInfo=[[NSMutableDictionary alloc] init];
                [viceInfo setValue:manager.nickName forKey:@"name"];
                AppDelegate *mydelegate=[[UIApplication sharedApplication] delegate];
                if(mydelegate!=NULL&&mydelegate.unbindViceAdminName!=NULL&&[mydelegate.unbindViceAdminName isEqualToString:manager.nickName])
                {
                    
                }
                else
                {
                    [[NSNotificationCenter defaultCenter] postNotificationName:VICEADMINUNBINDDEVICE object:nil  userInfo:viceInfo];
                    if(mydelegate!=NULL)
                        mydelegate.unbindViceAdminName=manager.nickName;
                }
            }
            else
            {
               nsql=[NSString stringWithFormat:@"update manager set phone=\"%@\",alias=\"%@\",birth=\"%@\",nick_name=\"%@\",update_time=\"%@\" ,is_sos=%d,group_id=%d where user_id=%@ and deviceuid=%@ and account_id=%@;",manager.phone,manager.alias,manager.birth,manager.nickName,manager.update_time,manager.isSos,manager.groupId,manager.user_id,manager.deviceUid,manager.accountId];
            }
        }
        else//管理员不存在
        {
            NSLog(@"%@,updateManagers,uid is %@, not exist,action is %@.",DATAMANAGER_TAG,manager.user_id,manager.action);
            if(manager.action!=NULL&& [manager.action intValue]==0)//已解绑
            {
              
            }
            else
            {
                nsql=[NSString stringWithFormat:@"insert into manager(user_id,alias,birth,phone,name,update_time,deviceuid,type,account_id,group_id,nick_name,is_sos)  values (%@,\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",%@,%d,%@,%d,\"%@\",%d);",manager.user_id,manager.alias,manager.birth,manager.phone,manager.name,manager.update_time,manager.deviceUid,manager.type,manager.accountId,manager.groupId,manager.nickName,manager.isSos];
                AppDelegate *mydelegate=[[UIApplication sharedApplication] delegate];
                if(mydelegate!=NULL)
                {
                    if(manager.type==VICE_ADMIN &&mydelegate.isReceiveBindRelationChange==YES)
                    {
                        /*
                        if(mydelegate.bindViceAdminUid!=NULL&&[mydelegate.bindViceAdminUid isEqualToString:manager.user_id])
                        {
                            
                        }
                        else
                        {*/
                            NSString *nsql5=[NSString stringWithFormat:@"insert into message(msg_type,deviceUid,nick_name,create_time,account_id) values (%d,%@,\"%@\",\"%@\",%@);",PT_VICEADMIN_JOIN,manager.deviceUid,manager.nickName,[Tooler getTS],manager.accountId];
                            [sqlArray addObject:nsql5];
                            mydelegate.bindViceAdminUid=manager.user_id;
                            Watch *watch=[self getWatch:manager.deviceUid];
                            UILocalNotification *localNotification = [[UILocalNotification alloc] init];
                            if(watch!=NULL&&watch.nickname!=NULL)
                               localNotification.alertBody = [NSString stringWithFormat:@"%@成为%@的副管理员",manager.nickName,watch.nickname];
                            else
                              localNotification.alertBody = [NSString stringWithFormat:@"%@成为手表的副管理员",manager.nickName];
                            localNotification.alertAction = @"确定";
                            localNotification.soundName = UILocalNotificationDefaultSoundName;
                            localNotification.hasAction=YES;
                            NSMutableDictionary *userInfo=[[NSMutableDictionary alloc] init];
                            [userInfo setValue:[NSString stringWithFormat:@"%d",PT_VICEADMIN_JOIN] forKey:@"type"];
                            localNotification.userInfo=userInfo;
                            [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
                            localNotification=NULL;
                            [[NSNotificationCenter defaultCenter] postNotificationName:VICEADMINBINDDEVICE object:nil  userInfo:nil];
                            
                        //}
                        //mydelegate.isReceiveBindRelationChange=NO;
                    }

                }
            }
        }
        if(nsql!=NULL)
        {
                [sqlArray addObject:nsql];
        }
        nsql=NULL;
        
    }
    
    if(sqlArray==NULL||[sqlArray count]==0)
    {
        NSLog(@"%@,updateManagers,sqlArray is null or count is 0",DATAMANAGER_TAG);
        return NO;
    }
    BOOL result=[self.theHelper execSql:sqlArray];
    if(result)
    {
        NSLog(@"%@,updateManagers success",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateManagers fail",DATAMANAGER_TAG);
    }
    return result;

}

-(BOOL)updateContacts:(NSArray *)contactLists
{
    NSLog(@"%@,updateContacts entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NO;
    if(contactLists==NULL||[contactLists count]==0 )
    {
        NSLog(@"%@,updateContacts,contactLists is null.",DATAMANAGER_TAG);
        return NO;
    }
    NSMutableArray *sqlArray=[[NSMutableArray alloc] init];
    if(sqlArray==NULL)
    {
        NSLog(@"%@,updateContacts,sqlArray is null.",DATAMANAGER_TAG);
        return NO;
    }
    [sqlArray addObject:@"PRAGMA foreign_keys = ON;"];

    for(int i=0;i<[contactLists count];i++)
    {
        Contact *contact=[contactLists objectAtIndex:i];
        if(contact==NULL||contact.user_id==NULL)
        {
            NSLog(@"%@,updateContacts,contact is null, index is %d",DATAMANAGER_TAG,i);
            continue;
        }
        // 判断联系人是否存在
        NSString *nsql1=[NSString stringWithFormat:@"select * from manager where user_id=\"%@\" and deviceuid=%@ and account_id=%@ and (type=%d or type=%d);",contact.user_id,contact.deviceUid,contact.accountId,CONTACT,SOS_CONTACT];
        const char *sql1=[nsql1 UTF8String];
        NSArray *data1=[self.theHelper searchData:sql1];
        NSString *nsql=NULL;
        if(data1!=NULL&&[data1 count]>0)
        {
            //联系人已存在
            NSLog(@"%@,updateContacts, the contact already exist, phone is %@.",DATAMANAGER_TAG,contact.phoneNum);
            if(contact.action!=NULL&&[contact.action intValue]==0)//已解绑
            {
                nsql=[NSString stringWithFormat:@"delete from manager where user_id=\"%@\" and deviceuid=%@ and account_id=%@ and (type=%d or type=%d);",contact.user_id,contact.deviceUid,contact.accountId,CONTACT,SOS_CONTACT];
            }
            else
            {
                //更新联系人信息
                nsql=[NSString stringWithFormat:@"update manager set name=\"%@\",phone=\"%@\",type=%d,update_time=\"%@\" where user_id=%@ and (type=%d or type=%d) and account_id=%@;",contact.contactname,contact.phoneNum,contact.type,contact.update_time,contact.user_id,CONTACT,SOS_CONTACT,contact.accountId];
            }
            
        }
        else
        {
            NSLog(@"%@,updateContacts, the contact not exist, phone is %@.",DATAMANAGER_TAG,contact.phoneNum);
            //联系人不存在
            if(contact.action!=NULL&&[contact.action intValue]==0)//已解绑
            {
                
            }
            else
            {
               nsql=[NSString stringWithFormat:@"insert into manager (phone,name,update_time,deviceuid,account_id,type,user_id) values (\"%@\",\"%@\",\"%@\",%@,%@,%d,%@);",contact.phoneNum,contact.contactname,contact.update_time,contact.deviceUid,contact.accountId,contact.type,contact.user_id];
            }
        }
        if(nsql!=NULL)
        {
           [sqlArray addObject:nsql];
        }

        nsql=NULL;
    }
    
    if(sqlArray==NULL||[sqlArray count]==0)
    {
        NSLog(@"%@,updateContacts,sqlArray is null or count is 0",DATAMANAGER_TAG);
        return NO;
    }
    BOOL result=[self.theHelper execSql:sqlArray];
    if(result)
    {
        NSLog(@"%@,updateContacts success",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateContacts fail",DATAMANAGER_TAG);
    }
    return result;
    
}

-(BOOL)unbindDevice:(NSString *)deviceUid uid:(NSString *)uid accountId:(NSString *)accountId
{
    NSLog(@"%@,unbindDevice entered.deviceUid is %@,uid is %@,acconuntId is %@.",DATAMANAGER_TAG,deviceUid,uid,accountId);
    if([self checkValid]==NO)
        return NO;
    if(deviceUid==NULL||!([deviceUid intValue]>0)||uid==NULL||!([uid intValue]>0)||accountId==NULL||!([accountId intValue]>0))
    {
        NSLog(@"%@,unbindDevice,parametesr is illegal",DATAMANAGER_TAG);
        return NO;
    }
    //删除绑定关系
    NSString *nsql1=[NSString stringWithFormat:@"delete from  manager where deviceuid=%@  and user_id=%@ and account_id=%@;",deviceUid,uid,accountId];
    BOOL result1=[self.theHelper deleteData:[nsql1 UTF8String] tableName:TABLE_bind];
    if(!result1)
    {
        NSLog(@"%@,unbindDevice,delete from manager fail",DATAMANAGER_TAG);
        return result1;
    }
    else
    {
        NSLog(@"%@,unbindDevice,delete from manager sucess",DATAMANAGER_TAG);
        //查看该手表是否还存在绑定关系
        NSString *nsql2=[NSString stringWithFormat:@"select * from manager where deviceuid=%@;",deviceUid];
        NSArray *data2=[self.theHelper searchData:[nsql2 UTF8String]];
        if(data2!=NULL&&[data2 count]>0)
        {
            //手表还存在绑定关系
            NSLog(@"%@,other manager bind the watch",DATAMANAGER_TAG);
        }
        else
        {
            // 手表不存在绑定关系，删除手表信息
            NSString *nsql3=[NSString stringWithFormat:@"delete from watch where deviceuid=%@;",deviceUid];
            NSMutableArray *sqlArray3=[[NSMutableArray alloc] init];
            [sqlArray3 addObject:@"PRAGMA foreign_keys=ON;"];
            [sqlArray3 addObject:nsql3];
            BOOL result2=[self.theHelper execSql:sqlArray3];
            if(!result2)
            {
                NSLog(@"%@,delete from watch failed.",DATAMANAGER_TAG);
            }
            else
            {
                NSLog(@"%@,delete from watch sucess.",DATAMANAGER_TAG);
            }
            return result2;
        }
    }
    return YES;
}
//主管理员解除绑定
-(BOOL)adminUnbindDevice:(NSString *)deviceUid groupId:(NSString *)groupId accountId:(NSString *)accountId
{
    NSLog(@"%@,adminUnbindDevice entered.deviceUid is %@,groupId is %@,acconuntId is %@.",DATAMANAGER_TAG,deviceUid,groupId,accountId);
    if([self checkValid]==NO)
        return NO;
    if(deviceUid==NULL||!([deviceUid intValue]>0)||groupId==NULL||!([groupId intValue]>0)||accountId==NULL||!([accountId intValue]>0))
    {
        NSLog(@"%@,adminUnbindDevice,parametesr is illegal",DATAMANAGER_TAG);
        return NO;
    }
    //删除家庭圈里所有绑定那个手表的管理员、发送的或接收的聊天消息
    NSString *nsql6=[NSString stringWithFormat:@"select * from watch where group_id=%@ and account_id=%@;",groupId,accountId];
    NSArray *data6=[self.theHelper searchData:[nsql6 UTF8String]];
    if(data6!=NULL&&[data6 count]>1)
    {
        
    }
    else
    {
        //NSString *nsql5=[NSString stringWithFormat:@"delete from chart where user_id in (select user_id from manager where deviceuid=%@  and group_id=%@ and account_id=%@ and user_id<>%@) or targetuid in (select user_id from manager where deviceuid=%@  and group_id=%@ and account_id=%@ and user_id<>%@);",deviceUid,groupId,accountId,accountId,deviceUid,groupId,accountId,accountId];
        //[self.theHelper deleteData:[nsql5 UTF8String] tableName:TABLE_CHART];
        NSString *nsql7=[NSString stringWithFormat:@"delete from chart where group_id=%@ and account_id=%@;",groupId,accountId];
        [self.theHelper deleteData:[nsql7 UTF8String] tableName:TABLE_CHART];
    }
    
    //删除绑定关系，删除该账号家庭圈里，所有绑定该手表的管理员
    NSString *nsql1=[NSString stringWithFormat:@"delete from  manager where deviceuid=%@  and account_id=%@;",deviceUid,accountId];
    BOOL result1=[self.theHelper deleteData:[nsql1 UTF8String] tableName:TABLE_bind];
    if(result1)
    {
        NSLog(@"%@,adminUnbindDevice,delete from manager sucess",DATAMANAGER_TAG);
    }
        //删除手表
        NSString *nsql3=[NSString stringWithFormat:@"delete from watch where deviceuid=%@ and account_id=%@;",deviceUid,accountId];
        NSMutableArray *sqlArray3=[[NSMutableArray alloc] init];
        [sqlArray3 addObject:nsql3];
        //删除该账号里，这个手表发的或收的所有聊天消息
        NSString *nsql4=[NSString stringWithFormat:@"delete from chart where (user_id=%@   or targetuid=%@) and account_id=%@;",deviceUid,deviceUid,accountId];
        [sqlArray3 addObject:nsql4];
        BOOL result2=[self.theHelper execSql:sqlArray3];
        if(!result2)
        {
            NSLog(@"%@,adminUnbindDevice from watch failed.",DATAMANAGER_TAG);
        }
        else
        {
            NSLog(@"%@,adminUnbindDevice from watch sucess.",DATAMANAGER_TAG);
            [self clearWatchData:deviceUid accountId:accountId];
        }
        return result2;
}

//副管理员解除绑定,直接退出家庭圈
-(BOOL)viceAdminUnbindDevice:(NSString *)userId groupId:(NSString *)groupId accountId:(NSString *)accountId
{
    NSLog(@"%@,viceAdminUnbindDevice userId is %@,groupId is %@,acconuntId is %@.",DATAMANAGER_TAG,userId,groupId,accountId);
    if([self checkValid]==NO)
        return NO;
    if(userId==NULL||!([userId intValue]>0)||groupId==NULL||!([groupId intValue]>0)||accountId==NULL||!([accountId intValue]>0))
    {
        NSLog(@"%@,viceAdminUnbindDevice,parametesr is illegal",DATAMANAGER_TAG);
        return NO;
    }
    [self clearGroupWatchData:accountId groupId:groupId];
    NSMutableArray *sqlArray3=[[NSMutableArray alloc] init];
    //直接退出家庭圈，删除聊天消息
    //NSString *nsql4=[NSString stringWithFormat:@"delete from chart where user_id in (select user_id from manager where  group_id=%@ and account_id=%@ and user_id<>%@) or targetuid in (select user_id from manager where  group_id=%@ and account_id=%@ and user_id<>%@);",groupId,accountId,accountId,groupId,accountId,accountId];
    //[sqlArray3 addObject:nsql4];//副管理员和其他管理员单聊信息不删除
    NSString *nsql6=[NSString stringWithFormat:@"delete from chart where (user_id in (select deviceuid from watch where  group_id=%@ and account_id=%@) and group_id=0) or targetuid in (select deviceuid from watch where  group_id=%@ and account_id=%@ );",groupId,accountId,groupId,accountId];
    [sqlArray3 addObject:nsql6];//只删除手表单聊消息
    //NSString *nsql5=[NSString stringWithFormat:@"delete from chart where group_id=%@;",groupId];
    //[sqlArray3 addObject:nsql5];//家庭圈消息不删除
    BOOL result2=[self.theHelper execSql:sqlArray3];
    if(!result2)
    {
        NSLog(@"%@,viceAdminUnbindDevice delete chart failed.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,viceAdminUnbindDevice delete chart sucess.",DATAMANAGER_TAG);
    }
    NSMutableArray *sqlArray4=[[NSMutableArray alloc] init];
    NSString *nsql1=[NSString stringWithFormat:@"delete from  manager where  group_id=%@ and account_id=%@;",groupId,accountId];
    [sqlArray4 addObject:nsql1];
    //删除该账号里，家庭圈中所有手表
    NSString *nsql3=[NSString stringWithFormat:@"delete from watch where group_id=%@ and account_id=%@;",groupId,accountId];
    [sqlArray4 addObject:nsql3];
    BOOL result1=[self.theHelper execSql:sqlArray4];
    if(!result2)
    {
        NSLog(@"%@,viceAdminUnbindDevice delete manager and watch failed.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,viceAdminUnbindDevice delete manager and watch sucess.",DATAMANAGER_TAG);
    }
    return result1;
}

-(BOOL)clearWatchData:(NSString *)deviceUid accountId:(NSString *)accountId
{
    NSLog(@"%@,clearWatchData entered,deviceUid is %@,accountId is %@.",DATAMANAGER_TAG,deviceUid,accountId);
    if([self checkValid]==NO)
        return NO;
    if(deviceUid==NULL||[deviceUid intValue]<=0||accountId==NULL||!([accountId intValue]>0))
    {
        NSLog(@"%@,clearWatchData,parameters is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    NSMutableArray *sqlArray=[[NSMutableArray alloc] init];
    if(sqlArray==NULL)
    {
        NSLog(@"%@,clearWatchData,sqlArray is null.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql1=[NSString stringWithFormat:@"delete from grow where deviceuid=%@;",deviceUid];
    [sqlArray addObject:nsql1];
    NSString *nsql2=[NSString stringWithFormat:@"delete from task where target_uid=%@;",deviceUid];
    [sqlArray addObject:nsql2];
    NSString *nsql3=[NSString stringWithFormat:@"delete from position where deviceuid=%@ and account_id=%@;",deviceUid,accountId];
    [sqlArray addObject:nsql3];
    NSString *nsql4=[NSString stringWithFormat:@"delete from fence where deviceuid=%@;",deviceUid];
    [sqlArray addObject:nsql4];
    NSString *nsql5=[NSString stringWithFormat:@"delete from message where  deviceuid=%@ and  account_id=%@;",deviceUid,accountId];
    [sqlArray addObject:nsql5];
    NSString *nsql6=[NSString stringWithFormat:@"delete from event where deviceuid=%@;",deviceUid];
    [sqlArray addObject:nsql6];
    NSString *nsql7=[NSString stringWithFormat:@"delete from remind where deviceuid=%@;",deviceUid];
    [sqlArray addObject:nsql7];
    NSString *nsql8=[NSString stringWithFormat:@"delete from manager where deviceuid=%@ and  account_id=%@ and (type=%d or type=%d);",deviceUid,accountId,CONTACT,SOS_CONTACT];
    [sqlArray addObject:nsql8];
    BOOL result=[self.theHelper execSql:sqlArray];
    if(result)
    {
        NSLog(@"%@,clearWatchData success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,clearWatchData fail",DATAMANAGER_TAG);
    }
    return result;
    
}

-(BOOL)clearGroupWatchData:(NSString *)accountId groupId:(NSString *)groupId
{
    NSLog(@"%@,clearWatchData entered,accountId is %@.groupId is %@.",DATAMANAGER_TAG,accountId,groupId);
    if([self checkValid]==NO)
        return NO;
    if(accountId==NULL||!([accountId intValue]>0)||groupId==NULL)
    {
        NSLog(@"%@,clearWatchData,parameters is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    NSMutableArray *sqlArray=[[NSMutableArray alloc] init];
    if(sqlArray==NULL)
    {
        NSLog(@"%@,clearWatchData,sqlArray is null.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql1=[NSString stringWithFormat:@"delete from grow where deviceuid in  (select deviceuid from watch where group_id=%@);",groupId];
    [sqlArray addObject:nsql1];
    NSString *nsql2=[NSString stringWithFormat:@"delete from task where target_uid in  (select deviceuid from watch where group_id=%@);",groupId];
    [sqlArray addObject:nsql2];
    NSString *nsql3=[NSString stringWithFormat:@"delete from position where deviceuid in  (select deviceuid from watch where group_id=%@) and account_id=%@;",groupId,accountId];
    [sqlArray addObject:nsql3];
    NSString *nsql4=[NSString stringWithFormat:@"delete from fence where deviceuid in  (select deviceuid from watch where group_id=%@);",groupId];
    [sqlArray addObject:nsql4];
    NSString *nsql5=[NSString stringWithFormat:@"delete from message where deviceuid in  (select deviceuid from watch where group_id=%@) and  account_id=%@;",groupId,accountId];
    [sqlArray addObject:nsql5];
    NSString *nsql6=[NSString stringWithFormat:@"delete from event where deviceuid in  (select deviceuid from watch where group_id=%@);",groupId];
    [sqlArray addObject:nsql6];
    NSString *nsql7=[NSString stringWithFormat:@"delete from remind where deviceuid in  (select deviceuid from watch where group_id=%@);",groupId];
    NSString *nsql8=[NSString stringWithFormat:@"delete from manager where deviceuid in  (select deviceuid from watch where group_id=%@) and  account_id=%@ and (type=%d or type=%d);",groupId,accountId,CONTACT,SOS_CONTACT];
    [sqlArray addObject:nsql8];
    [sqlArray addObject:nsql7];
    BOOL result=[self.theHelper execSql:sqlArray];
    if(result)
    {
        NSLog(@"%@,clearWatchData success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,clearWatchData fail",DATAMANAGER_TAG);
    }
    return result;
    
}

-(BOOL)updateLocateParameters:(NSString *)deviceUid reportInterval:(float)reportInterval sampleInterval:(float)sampleInterval
{
    NSLog(@"%@,updateLocateParameters entered,deviceUid is %@,reportInterval is %f,sampleInterval is %f.",DATAMANAGER_TAG,deviceUid,reportInterval,sampleInterval);
    if([self checkValid]==NO)
        return NO;
    if(deviceUid==NULL||[deviceUid intValue]<=0||reportInterval<=0||sampleInterval<=0)
    {
        NSLog(@"%@,updateLocateParameters,parameters is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql1=[NSString stringWithFormat:@"update watch set report_interval=%f,sample_interval=%f where deviceuid=%@",reportInterval,sampleInterval,deviceUid];
    BOOL result=[self.theHelper updateData:[nsql1 UTF8String] tableName:TABLE_WATCH];
    if(result)
    {
        NSLog(@"%@,updateLocateParameters sucess.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateLocateParameters fail.",DATAMANAGER_TAG);
    }
    return result;
}
-(BOOL)addFence:(Fence *)fence
{
    NSLog(@"%@,addFence entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NO;
    if(fence==NULL||fence.fence_id==NULL)
    {
        NSLog(@"%@,addFence,fence is null.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"insert into fence (fence_id,deviceuid,latitude,longtitude,altitude,description,radius,title,start_time,end_time,cycle,is_active,type) values (%@,%@,%f,%f,%f,\"%@\",%f,\"%@\",\"%@\",\"%@\",\"%@\",%d,%d);",fence.fence_id,fence.deviceUid,fence.latitude,fence.longtitude,fence.altitude,fence.desc,fence.radius,fence.title,fence.start_time,fence.end_time,fence.cycle,fence.is_active,fence.type];
    BOOL result=[self.theHelper insertData:[nsql UTF8String] tableName:TABLE_FENCE];
    if(result)
    {
        NSLog(@"%@,addFence success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,addFence fail.",DATAMANAGER_TAG);
    }
    return result;

}
-(BOOL)updateFence:(Fence *)fence
{
    NSLog(@"%@,updateFence entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NO;
    if(fence==NULL||[fence.fence_id intValue]<=0)
    {
        NSLog(@"%@,updateFences,fences is null",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"update fence set latitude=%f,longtitude=%f,altitude=%f,description=\"%@\",radius=%f,title=\"%@\",start_time=\"%@\",end_time=\"%@\",cycle=\"%@\",is_active=%d,type=%d where fence_id=%@ and deviceuid=%@;",fence.latitude,fence.longtitude,fence.altitude,fence.desc,fence.radius,fence.title,fence.start_time,fence.end_time,fence.cycle,fence.is_active,fence.type,fence.fence_id,fence.deviceUid];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_FENCE];
    if(result)
    {
        NSLog(@"%@,updateFence success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateFence fail.",DATAMANAGER_TAG);
    }
    return result;
    
}
-(BOOL)updateFences:(NSArray *)fences
{
    NSLog(@"%@,updateFences entered,fences is %@.",DATAMANAGER_TAG,fences);
    if([self checkValid]==NO)
        return NO;
    if(fences==NULL||[fences count]<=0)
    {
        NSLog(@"%@,updateFences,fences is null",DATAMANAGER_TAG);
        return NO;
    }
    NSMutableArray *sqlArray=[[NSMutableArray alloc] init];
    if(sqlArray==NULL)
    {
        NSLog(@"%@,updateFences,sqlArray is null.",DATAMANAGER_TAG);
        return NO;
    }
    for(int i=0;i<[fences count];i++)
    {
        Fence *fence=[fences objectAtIndex:i];
        if(fence==NULL||fence.fence_id==NULL||[fence.fence_id intValue]<=0)
        {
            NSLog(@"%@,updateFences,fence is null,index is %d.",DATAMANAGER_TAG,i);
            continue;
        }
        NSString *nsql1=[NSString stringWithFormat:@"select * from fence where fence_id=%@;",fence.fence_id];
        NSArray *data1=[self.theHelper searchData:[nsql1 UTF8String]];
        NSString *nsql2=NULL;
        if(data1!=NULL&&[data1 count]>0)//already exist
        {
            if(fence.action==0)
            {
                nsql2=[NSString stringWithFormat:@"delete from fence where fence_id=%@;",fence.fence_id];
            }
            else
            {
                nsql2=[NSString stringWithFormat:@"update fence set latitude=%f,longtitude=%f,altitude=%f,description=\"%@\",radius=%f,title=\"%@\",start_time=\"%@\",end_time=\"%@\",cycle=\"%@\",is_active=%d,type=%d,update_time=\"%@\" where fence_id=%@;",fence.latitude,fence.longtitude,fence.altitude,fence.desc,fence.radius,fence.title,fence.start_time,fence.end_time,fence.cycle,fence.is_active,fence.type,fence.updateTime,fence.fence_id];
            }
        }
        else//not exist
        {
            if(fence.action==1)
            {
                    nsql2=[NSString stringWithFormat:@"insert into fence (fence_id,deviceuid,latitude,longtitude,altitude,description,radius,title,start_time,end_time,cycle,is_active,type,update_time) values (%@,%@,%f,%f,%f,\"%@\",%f,\"%@\",\"%@\",\"%@\",\"%@\",%d,%d,\"%@\");",fence.fence_id,fence.deviceUid,fence.latitude,fence.longtitude,fence.altitude,fence.desc,fence.radius,fence.title,fence.start_time,fence.end_time,fence.cycle,fence.is_active,fence.type,fence.updateTime];
            }
        }
        if(nsql2!=NULL)
        {
            [sqlArray addObject:nsql2];
        }
    }
    if([sqlArray count]==0)
    {
        NSLog(@"%@,updateFences,has no sql to exec.",DATAMANAGER_TAG);
        return NO;
    }
    BOOL result=[self.theHelper execSql:sqlArray];
    if(result)
    {
        NSLog(@"%@,updateFences success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateFences fail.",DATAMANAGER_TAG);
    }
    return result;
    
}
-(BOOL)deleteFence:(NSString *)fenceId deviceUid:(NSString *)deviceUid
{
    NSLog(@"%@,deleteFence entered,fenceId is %@,deviceUid is %@.",DATAMANAGER_TAG,fenceId,deviceUid);
    if([self checkValid]==NO)
        return NO;
    if(fenceId==NULL||!([fenceId intValue]>0))
    {
        NSLog(@"%@,deleteFence,fenceId is null.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"delete from fence where fence_id=%@ and deviceuid=%@;",fenceId,deviceUid];
    BOOL result=[self.theHelper deleteData:[nsql UTF8String] tableName:TABLE_FENCE];
    if(result)
    {
        NSLog(@"%@,deleteFence success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,deleteFence fail.",DATAMANAGER_TAG);
    }
    return result;
}
-(long)getFenceId:(Fence *)fence
{
    NSLog(@"%@,getFenceId entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return 0;
    if(fence==NULL||[fence.deviceUid intValue]<=0)
    {
        NSLog(@"%@,getFenceId,fence is null.",DATAMANAGER_TAG);
        return 0;
    }
    NSString *nsql=[NSString stringWithFormat:@"insert into fence (deviceuid,latitude,longtitude,altitude,description,radius,title,start_time,end_time,cycle,is_active) values (%@,%f,%f,%f,\"%@\",%f,\"%@\",\"%@\",\"%@\",\"%@\",%d);",fence.deviceUid,fence.latitude,fence.longtitude,fence.altitude,fence.desc,fence.radius,fence.title,fence.start_time,fence.end_time,fence.cycle,fence.is_active];
    long fenceId=[self.theHelper insertDataRet:[nsql UTF8String] tableName:TABLE_FENCE];
    if(fenceId>0)
    {
        NSLog(@"%@,getFenceId, fenceId is %ld.",DATAMANAGER_TAG,fenceId);
    }
    else
    {
        NSLog(@"%@,getFenceId fail.",DATAMANAGER_TAG);
    }
    return fenceId;
}
-(BOOL)deleteFences:(NSArray *)fences
{
    NSLog(@"%@,deleteFences entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NO;
    if(fences==NULL||[fences count]<=0)
    {
        NSLog(@"%@,deleteFences,fences is null.",DATAMANAGER_TAG);
        return NO;
    }
    NSMutableArray *sqlArray=[[NSMutableArray alloc] init];
    if(sqlArray==NULL)
    {
        NSLog(@"%@,deleteFences,sqlArray is null.",DATAMANAGER_TAG);
        return NO;
    }
    for(int i=0;i<[fences count];i++)
    {
        Fence *theFence=[fences objectAtIndex:i];
        if(theFence==NULL)
        {
            NSLog(@"%@,deleteFences,theFence is null.",DATAMANAGER_TAG);
            continue;
        }
        NSString *nsql=[NSString stringWithFormat:@"delete from fence where id=%@;",theFence.fence_id];
        [sqlArray addObject:nsql];
        theFence=NULL;
        nsql=NULL;
    }
    if([sqlArray count]==0)
    {
        NSLog(@"%@,deleteFences, has no sql to exec",DATAMANAGER_TAG);
        return NO;
    }
    BOOL result=[self.theHelper execSql:sqlArray];
    if(result)
    {
        NSLog(@"%@,deleteFences success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,deleteFences fail.",DATAMANAGER_TAG);
    }
    return result;
}

-(BOOL)addPosition:(NSArray *)locArray
{
    NSLog(@"%@,addPosition entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NO;
    if(locArray==NULL||[locArray count]==0)
    {
        NSLog(@"%@,addPosition,locArray is null.",DATAMANAGER_TAG);
        return NO;
    }
    User *user=[User sharedSingleton];
    if(user==NULL||user.uid==NULL)
        return NO;
    NSMutableArray *sqlArray=[[NSMutableArray alloc] init];
    if(sqlArray==NULL)
    {
        NSLog(@"%@,addPosition,sqlArray is null.",DATAMANAGER_TAG);
        return NO;
    }
    for(int i=0;i<[locArray count];i++)
    {
        Location *location=[locArray objectAtIndex:i];
        if(location==NULL||[location.deviceUid intValue]<=0)
        {
            NSLog(@"%@,addPosition,location is null,index is %d.",DATAMANAGER_TAG,i);
            continue;
        }
        NSString *nsql1=[NSString stringWithFormat:@"select * from position where  deviceuid=%@ and gen_time=\"%@\";",location.deviceUid,location.gen_time];
        NSArray *data1=[self.theHelper searchData:[nsql1 UTF8String]];
        if(data1!=NULL&&[data1 count]>0)
            continue;
        int year=[Tooler getYear:location.gen_time];
        int month=[Tooler getMonth:location.gen_time];
        int day=[Tooler getDay:location.gen_time];
        NSString *nsql=[NSString stringWithFormat:@"insert into position (deviceuid,altitude,latitude,longtitude,radius,type,update_time,gen_time,locate_type,main_address,sub_address,year,month,day,account_id) values (%@,%f,%f,%f,%f,%d,\"%@\",\"%@\",%d,\"%@\",\"%@\",%d,%d,%d,%@);",location.deviceUid,location.altitude,location.latitude,location.longtitude,location.radius,location.type,location.updateTime,location.gen_time,location.locateType,location.mainAddress,location.subAddress,year,month,day,user.uid];
        [sqlArray addObject:nsql];
        location=NULL;
        nsql=NULL;
    }
    if([sqlArray count]==0)
    {
        NSLog(@"%@,addPosition, has no sql to exec.",DATAMANAGER_TAG);
        return NO;
    }
    @try {
        BOOL result=[self.theHelper execSql:sqlArray];
        if(result)
        {
            NSLog(@"%@,addPosition success.",DATAMANAGER_TAG);
        }
        else
        {
            NSLog(@"%@,addPosition fail.",DATAMANAGER_TAG);
        }
        return result;
    }
    @catch (NSException *exception) {
        return NO;
    }
    @finally {
        
    }

}

-(NSString *)getlatestLocTime:(NSString *)deviceUid
{
    NSLog(@"%@,getlatestLocTime entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NULL;
    if(deviceUid==NULL||!([deviceUid intValue]>0))
    {
        NSLog(@"%@,getlatestLocTime,deviceUid is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select update_time from position where deviceuid=%@ order by update_time desc limit 1;",deviceUid];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getlatestLocTime,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSDictionary *info=[data objectAtIndex:0];
    if(info==NULL||[info count]==0)
    {
        NSLog(@"%@,getlatestLocTime,info is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *ts=[info valueForKey:@"gen_time"];
    return ts;
   
}
-(int)addLocation:(Location *)location
{
    NSLog(@"%@,addLocation entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return -1;
    if(location==NULL)
    {
        NSLog(@"%@,addLocation,location is null.",DATAMANAGER_TAG);
        return -1;
    }
    User *user=[User sharedSingleton];
    if(user==NULL||user.uid==NULL)
        return -1;
    NSString *nsql1=[NSString stringWithFormat:@"select * from position where  deviceuid=%@ and gen_time=\"%@\";",location.deviceUid,location.gen_time];
    NSArray *data1=[self.theHelper searchData:[nsql1 UTF8String]];
    
    if(data1!=NULL&&[data1 count]>0)
    {
        /*
        NSDictionary *locInfo=[data1 objectAtIndex:0];
        if(locInfo!=NULL&&[locInfo count]>0)
        {
            int locId=[[locInfo valueForKey:@"id"] intValue];
            if(locId>0)
                return locId;
        }*/
        return -1;
    }
    int year=[Tooler getYear:location.gen_time];
    int month=[Tooler getMonth:location.gen_time];
    int day=[Tooler getDay:location.gen_time];
    NSString *ts=[Tooler getTS];
    NSString *nsql=[NSString stringWithFormat:@"insert into position (deviceuid,latitude,longtitude,radius,type,update_time,gen_time,locate_type,loc,main_address,sub_address,year,month,day,account_id) values (%@,%f,%f,%f,%d,\"%@\",\"%@\",%d,\"%@\",\"%@\",\"%@\",%d,%d,%d,%@);",location.deviceUid,location.latitude,location.longtitude,location.radius,location.type,ts,location.gen_time,location.locateType,location.loc,location.mainAddress,location.subAddress,year,month,day,user.uid];
    int ret=(int)[self.theHelper insertDataRet:[nsql UTF8String] tableName:TABLE_POSITION];
    return ret;
}
-(BOOL)addContact:(Contact *)contact
{
    NSLog(@"%@,addContact entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NO;
    if(contact==NULL||contact.phoneNum==NULL)
    {
        NSLog(@"%@,addContact,contact is null.",DATAMANAGER_TAG);
        return NO;
    }
    BOOL result=NO;
    NSString *nsql1=[NSString stringWithFormat:@"select * from manager where user_id=%@ and deviceuid=%@ and account_id=%@ and (type=%d or type=%d);",contact.user_id,contact.deviceUid,contact.accountId,CONTACT,SOS_CONTACT];
    NSArray *data1=[self.theHelper searchData:[nsql1 UTF8String]];
    if(data1!=NULL&&[data1 count]>0)
    {
        //联系人已存在
        NSLog(@"%@,addContact,the contact already exist.",DATAMANAGER_TAG);
        NSString *nsql2=[NSString stringWithFormat:@"update manager set name=\"%@\",pic_url=\"%@\",phone=\"%@\",photohash=\"%@\",update_time=\"%@\", type=%d where user_id=\"%@\" and deviceuid=%@ and account_id=%@ and (type=%d or type=%d);",contact.contactname,contact.photo_url,contact.phoneNum,contact.photoHash,contact.update_time,contact.type,contact.user_id,contact.deviceUid,contact.accountId,CONTACT,SOS_CONTACT];
        result=[self.theHelper updateData:[nsql2 UTF8String] tableName:TABLE_MANAGER];
    }
    else
    {
        NSString *nsql3=[NSString stringWithFormat:@"insert into manager (pic_url,phone,name,update_time,deviceuid,type,account_id,user_id,photohash) values (\"%@\",\"%@\",\"%@\",\"%@\",%@,%d,%@,%@,\"%@\");",contact.photo_url,contact.phoneNum,contact.contactname,contact.update_time,contact.deviceUid,contact.type,contact.accountId,contact.user_id,contact.photoHash];
        result=[self.theHelper insertData:[nsql3 UTF8String] tableName:TABLE_bind];
    }
    if(result)
    {
        NSLog(@"%@,addContact success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,addContact fail.",DATAMANAGER_TAG);
    }
    return result;
    
}
-(BOOL)deleteContact:(Contact *)contact
{
    NSLog(@"%@,deleteContact entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NO;
    if(contact==NULL||contact.user_id==NULL)
    {
        NSLog(@"%@,deleteContact,contact is null.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"delete from manager where user_id=%@ and deviceuid=%@ and account_id=%@  and (type=3 or type=4);",contact.user_id,contact.deviceUid,contact.accountId];
    BOOL result=[self.theHelper deleteData:[nsql UTF8String] tableName:TABLE_MANAGER];
    if(result)
    {
        NSLog(@"%@,deleteContact success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,deleteContact fail.",DATAMANAGER_TAG);
    }
    return result;
}
/*
-(BOOL)updateManager:(Manager *)manager
{
    NSLog(@"%@,updateManager entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NO;
    if(manager==NULL||manager.user_id==NULL||!([manager.user_id intValue]>0))
    {
        NSLog(@"%@,updateManager,parameters is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from manager where user_id=%@;",manager.user_id ];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    BOOL result=NO;
    NSString *pic_url=[NSString stringWithFormat:@"/headimg/%@",manager.photoName];
    if(data!=NULL&&[data count]>0)
    {
        NSLog(@"%@,updateManager, manager already exists, uid is %@.",DATAMANAGER_TAG,manager.user_id);
        NSString *nsql2=[NSString stringWithFormat:@"update manager set birth=\"%@\",age=%d,email=\"%@\",address=\"%@\",alias=\"%@\",group_id=%d,pic_url=\"%@\",photoHash=\"%@\" where user_id=%@; ",manager.birth,manager.age,manager.email,manager.address,manager.alias,manager.groupId,pic_url,manager.photoHash,manager.user_id];
        result=[self.theHelper updateData:[nsql2 UTF8String] tableName:TABLE_MANAGER];
    }
    else
    {
       NSLog(@"%@,updateManager, manager not exists, uid is %@.",DATAMANAGER_TAG,manager.user_id);
       NSString *nsql3=[NSString stringWithFormat:@"insert into manager(user_id,alias,pic_url,birth,phone,name,update_time,photoHash,deviceUid,type,account_id,group_id,age,email,address)  values (%@,\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",%@,%d,%@,%d,%d,\"%@\",\"%@\");",manager.user_id,manager.alias,pic_url,manager.birth,manager.phone,manager.name,manager.update_time,manager.photoHash,manager.deviceUid,manager.type,manager.accountId,manager.groupId,manager.age,manager.email,manager.address];
        result=[self.theHelper insertData:[nsql3 UTF8String] tableName:TABLE_MANAGER];
    }
    if(result)
    {
        NSLog(@"%@,updateManager success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateManager fail.",DATAMANAGER_TAG);
    }
    return result;
}
 */
-(BOOL)addChat:(Chat *)chat
{
    NSLog(@"%@,addChat entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NO;
    if(chat==NULL||chat.user_id==NULL||[chat.user_id intValue]<=0)
    {
        NSLog(@"%@,addChat chat is null.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *fileUrl=[NSString stringWithFormat:@"/record/%@",chat.file_name];
    NSString *amrUrl=[NSString stringWithFormat:@"/record/%@",chat.amr_name];
    NSString *nsql=[NSString stringWithFormat:@"insert into chart (user_id,file_url,duration,gen_time,update_time,group_id,deviceuid,account_id,type,targetuid,is_read,is_finish,amr_url,is_send) values (%@,\"%@\",%d,\"%@\",\"%@\",%d,%@,%@,%d,%@,%d,%d ,\"%@\",%d);",chat.user_id,fileUrl,chat.duration,chat.genTime,[Tooler getTS],chat.groupId,chat.deviceUid,chat.accountId,chat.type,chat.targetUid,chat.is_read,chat.is_finish,amrUrl,chat.is_send];
    BOOL result=[self.theHelper insertData:[nsql UTF8String] tableName:TABLE_CHART];
    if(result)
    {
        NSLog(@"%@,addChat success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,addChat fail.",DATAMANAGER_TAG);
    }
    return result;    
}
-(BOOL)addPushChat:(Chat *)chat
{
    NSLog(@"%@,addPushChat entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NO;
    if(chat==NULL||chat.user_id==NULL||[chat.user_id intValue]<=0)
    {
        NSLog(@"%@,addPushChat chat is null.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql1=NULL;
    if(chat.groupId>0)
    {
        nsql1=[NSString stringWithFormat:@"select * from chart where group_id=%d and seqid=%@ and account_id=%@;",chat.groupId,chat.sqeId,chat.accountId];
    }
    else
    {
        nsql1=[NSString stringWithFormat:@"select * from chart where targetuid=%@ and user_id=%@ and seqid=%@ and account_id=%@;",chat.targetUid,chat.user_id,chat.sqeId,chat.accountId];
    }
    NSArray *data1=[self.theHelper searchData:[nsql1 UTF8String]];
    if(data1!=NULL&&[data1 count]>0)
        return NO;
    //NSString *fileUrl=[NSString stringWithFormat:@"/record/%@",chat.file_name];
    NSString *nsql=[NSString stringWithFormat:@"insert into chart (user_id,duration,gen_time,update_time,group_id,deviceuid,account_id,type,targetuid,is_read,is_finish,seqid,is_send,is_view) values (%@,%d,\"%@\",\"%@\",%d,%@,%@,%d,%@,%d,%d,%@,%d,%d );",chat.user_id,chat.duration,chat.genTime,[Tooler getTS],chat.groupId,chat.deviceUid,chat.accountId,chat.type,chat.targetUid,chat.is_read,chat.is_finish,chat.sqeId,chat.is_send,chat.is_view];
    BOOL result=[self.theHelper insertData:[nsql UTF8String] tableName:TABLE_CHART];
    if(result)
    {
        NSLog(@"%@,addPushChat success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,addPushChat fail.",DATAMANAGER_TAG);
    }
    return result;
}

-(BOOL)updateChat:(Chat *)chat
{
    NSLog(@"%@,updateChat entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NO;
    if(chat==NULL||!([chat.chatId intValue]>0))
    {
        NSLog(@"%@,updateChat chat is null.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"update chart set seqid=%@,is_send=%d where id=%@;",chat.sqeId,chat.is_send,chat.chatId];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_CHART];
    if(result)
    {
        NSLog(@"%@,updateChat success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateChat fail.",DATAMANAGER_TAG);
    }
    return result;
}
-(NSString *)getseqId:(int) groupId
{
    NSLog(@"%@,getseqId entered. groupId is %d.",DATAMANAGER_TAG,groupId);
    if([self checkValid]==NO)
        return NULL;
    if(groupId<=0)
    {
        NSLog(@"%@,getseqId, groupId is illegal.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *seqId=NULL;
    NSString *nsql=[NSString stringWithFormat:@"select seqid  from chart where group_id=%d order by seqid desc limit 1;",groupId];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data!=NULL&&[data count]>0)
    {
        NSDictionary *info=[data objectAtIndex:0];
        if(info==NULL||[info count]==0)
        {
            NSLog(@"%@,getseqId,info is null.",DATAMANAGER_TAG);
            return NULL;
        }
        seqId=[info valueForKey:@"seqid"];
    }
    return seqId;
}
-(NSString *)getseqIdSingle:(NSString *) targetUid//84
{
    NSLog(@"%@,getseqIdSingle entered. groupId is %@.",DATAMANAGER_TAG,targetUid);
    if([self checkValid]==NO)
        return NULL;
    if([targetUid intValue]<=0)
    {
        NSLog(@"%@,getseqIdSingle, groupId is illegal.",DATAMANAGER_TAG);
        return NULL;
    }
    User *user=[User sharedSingleton];
    if(user==NULL||user.uid==NULL)
        return NULL;
    NSString *seqId=NULL;
    NSString *nsql=[NSString stringWithFormat:@"select seqid from chart where (targetuid=%@ and user_id=%@) or (targetuid=%@ and user_id=%@) and account_id=%@ order by seqid desc limit 1;",targetUid,user.uid,user.uid,targetUid,user.uid];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data!=NULL&&[data count]>0)
    {
        NSDictionary *info=[data objectAtIndex:0];
        if(info==NULL||[info count]==0)
        {
            NSLog(@"%@,getseqIdSingle,info is null.",DATAMANAGER_TAG);
            return NULL;
        }
        seqId=[info valueForKey:@"seqid"];
    }
    return seqId;
}

-(BOOL)addChats:(NSArray *)chatList
{
    NSLog(@"%@,addChats entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NO;
    if(chatList==NULL||[chatList count]==0)
    {
        NSLog(@"%@,addChats,chatList is null.",DATAMANAGER_TAG);
        return NO;
    }
    NSMutableArray *sqlArray=[[NSMutableArray alloc] init];
    if(sqlArray==NULL)
    {
        NSLog(@"%@,addChats,sqlArray is null.",DATAMANAGER_TAG);
        return NO;
    }
    for(int i=0;i<[chatList count];i++)
    {
        Chat *chat=[chatList objectAtIndex:i];
        if(chat==NULL)
        {
            NSLog(@"%@,addChats,chat is null, index is %d.",DATAMANAGER_TAG,i);
            continue;
        }
        //NSString *fileUrl=[NSString stringWithFormat:@"/record/%@",chat.file_name];
        NSString *nsql1=NULL;
        if(chat.groupId>0)
        {
           nsql1=[NSString stringWithFormat:@"select * from chart where group_id=%d and seqid=%@ and account_id=%@;",chat.groupId,chat.sqeId,chat.accountId];
        }
        else
        {
            nsql1=[NSString stringWithFormat:@"select * from chart where targetuid=%@ and user_id=%@ and seqid=%@ and account_id=%@;",chat.targetUid,chat.user_id,chat.sqeId,chat.accountId];
        }
        NSArray *data1=[self.theHelper searchData:[nsql1 UTF8String]];
        if(data1!=NULL&&[data1 count]>0)
            continue;
       NSString *nsql=[NSString stringWithFormat:@"insert into chart (user_id,duration,gen_time,update_time,group_id,deviceuid,account_id,type,targetuid,seqid,is_read,is_finish,is_view) values (%@,%d,\"%@\",\"%@\",%d,%@,%@,%d,%@,%@,%d,%d,%d );",chat.user_id,chat.duration,chat.genTime,chat.createTime,chat.groupId,chat.deviceUid,chat.accountId,chat.type,chat.targetUid,chat.sqeId,chat.is_read,chat.is_finish,chat.is_view];
        [sqlArray addObject:nsql];
        chat=NULL;
        nsql=NULL;
    }
    if([sqlArray count]==0)
    {
        NSLog(@"%@, has no sql to exec.",DATAMANAGER_TAG);
        return NO;
    }
    BOOL result=[self.theHelper execSql:sqlArray];
    if(result)
    {
        NSLog(@"%@,addContact success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,addContact fail.",DATAMANAGER_TAG);
    }
    return result;
}
-(BOOL)updateGroupChatFile:(NSString *)seqId filePath:(NSString *)filePath groupId:(NSString *)groupId
{
    NSLog(@"%@,updateGroupChatFile entered,seqId is %@,filePath is %@,groupId is %@.",DATAMANAGER_TAG,seqId,filePath,groupId);
    if([self checkValid]==NO)
        return NO;
    if(seqId==NULL||filePath==NULL||groupId==NULL)
    {
        NSLog(@"%@,updateGroupChatFile,parameters is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"update chart set file_url=\"%@\",is_finish=1  where seqid=%@ and group_id=%@;",filePath,seqId,groupId];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_WATCH];
    if(result)
    {
        NSLog(@"%@,updateGroupChatFile success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateGroupChatFile fail.",DATAMANAGER_TAG);
    }
    return result;
}
-(BOOL)updateSingleChatFile:(NSString *)seqId filePath:(NSString *)filePath targetUid:(NSString *)targetUid
{
    NSLog(@"%@,updateSingleChatFile entered,seqId is %@,filePath is %@,targetUid is %@.",DATAMANAGER_TAG,seqId,filePath,targetUid);
    if([self checkValid]==NO)
        return NO;
    if(seqId==NULL||filePath==NULL||targetUid==NULL)
    {
        NSLog(@"%@,updateSingleChatFile,parameters is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"update chart set file_url=\"%@\",is_finish=1  where seqid=%@ and targetuid=%@;",filePath,seqId,targetUid];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_WATCH];
    if(result)
    {
        NSLog(@"%@,updateSingleChatFile success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateSingleChatFile fail.",DATAMANAGER_TAG);
    }
    return result;
}

//获取定位的最后时间

//更新手表头像信息
-(BOOL)updateWatchPhoto:(NSString *)deviceUid filePath:(NSString *)filePath photoHash:(NSString *)photoHash
{
    NSLog(@"%@,updateWatchPhoto entered,deviceUid is %@,filePath is %@,photoHash is %@.",DATAMANAGER_TAG,deviceUid,filePath,photoHash);
    if([self checkValid]==NO)
        return NO;
    if(deviceUid==NULL)
    {
        NSLog(@"%@,updateWatchPhoto,parameters is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"update watch set pic_url=\"%@\",photohash=\"%@\" where deviceuid=%@;",filePath,photoHash,deviceUid];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_WATCH];
    if(result)
    {
        NSLog(@"%@,updateWatchPhoto success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateWatchPhoto fail.",DATAMANAGER_TAG);
    }
    return result;
}

//更新管理员头像信息
-(BOOL)updateManagerPhoto:(NSString *)user_id filePath:(NSString *)filePath photoHash:(NSString *)photoHash
{
    NSLog(@"%@,updateManagerPhoto entered,user_id is %@,filePath is %@,photoHash is %@.",DATAMANAGER_TAG,user_id,filePath,photoHash);
    if([self checkValid]==NO)
        return NO;
    if(user_id==NULL)
    {
        NSLog(@"%@,updateManagerPhoto,parameters is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"update manager set pic_url=\"%@\",photohash=\"%@\" where user_id=%@;",filePath,photoHash,user_id];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_WATCH];
    if(result)
    {
        NSLog(@"%@,updateManagerPhoto success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateManagerPhoto fail.",DATAMANAGER_TAG);
    }
    return result;
}
//更新联系人头像信息
-(BOOL)updateContactPhoto:(NSString *)userId filePath:(NSString *)filePath photoHash:(NSString *)photoHash
{
    NSLog(@"%@,updateContactPhoto entered,userId is %@,filePath is %@,photoHash is %@.",DATAMANAGER_TAG,userId,filePath,photoHash);
    if([self checkValid]==NO)
        return NO;
    if(userId==NULL)
    {
        NSLog(@"%@,updateContactPhoto,parameters is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"update manager set pic_url=\"%@\",photohash=\"%@\" where user_id=%@;",filePath,photoHash,userId];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_WATCH];
    if(result)
    {
        NSLog(@"%@,updateContactPhoto success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateContactPhoto fail.",DATAMANAGER_TAG);
    }
    return result;
}

-(NSString *)getWatchTS:(NSString *)accountId
{
    NSLog(@"%@,getWatchTS entered,accountId is %@.",DATAMANAGER_TAG,accountId);
    if([self checkValid]==NO)
        return NULL;
    if(accountId==NULL||!([accountId intValue]>0))
    {
        NSLog(@"%@,getWatchTS,accountId is illegal.",DATAMANAGER_TAG);
        return NULL;
    }
    //NSString *nsql=[NSString stringWithFormat:@"select update_time from watch where deviceUid in (select deviceUid from manager where account_id=%@) order by update_time desc limit 1;",accountId];
    NSString *nsql=[NSString stringWithFormat:@"select update_time from watch  where account_id=%@ order by update_time desc limit 1;",accountId];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getWatchTS,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSDictionary *info=[data objectAtIndex:0];
    if(info==NULL||[info count]==0)
    {
        NSLog(@"%@,getWatchTS,info is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *ts=[info valueForKey:@"update_time"];
    return ts;
    
}
-(NSString *)getEventTS:(NSString *)deviceUid
{
    NSLog(@"%@,getEventTS entered,deviceUid is %@.",DATAMANAGER_TAG,deviceUid);
    if([self checkValid]==NO)
        return NULL;
    if(deviceUid==NULL||!([deviceUid intValue]>0))
    {
        NSLog(@"%@,getEventTS,deviceUid is illegal.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select update_time from event where deviceuid=%@  order by update_time desc limit 1;",deviceUid];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getEventTS,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSDictionary *info=[data objectAtIndex:0];
    if(info==NULL||[info count]==0)
    {
        NSLog(@"%@,getEventTS,info is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *ts=[info valueForKey:@"update_time"];
    return ts;
    
}
-(NSString *)getLocationTS:(NSString *)deviceUid
{
    NSLog(@"%@,getLocationTS entered,deviceUid is %@.",DATAMANAGER_TAG,deviceUid);
    if([self checkValid]==NO)
        return NULL;
    if(deviceUid==NULL||!([deviceUid intValue]>0))
    {
        NSLog(@"%@,getLocationTS,deviceUid is illegal.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select update_time from event where deviceuid=%@  order by update_time desc limit 1;",deviceUid];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getLocationTS,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSDictionary *info=[data objectAtIndex:0];
    if(info==NULL||[info count]==0)
    {
        NSLog(@"%@,getLocationTS,info is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *ts=[info valueForKey:@"update_time"];
    return ts;
    
}
//添加静音时段
-(BOOL)addSilent:(Silent *)silent deviceUid:(NSString *)deviceUid
{
    NSLog(@"%@,addSilent entered,deviceUid is %@.",DATAMANAGER_TAG,deviceUid);
    if([self checkValid]==NO)
        return NO;
    if(deviceUid==NULL||silent==NULL||!([deviceUid intValue]>0))
    {
        NSLog(@"%@,addSilent,parameters is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"insert into event (silent_id,deviceuid,type,start_time,end_time,title,desc,cycle,is_active) values (%@,%@,%d,\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",%d);",silent.slient_id,deviceUid,silent.type,silent.startTime,silent.endTime,silent.title,silent.desc,silent.cycle,silent.active];
    BOOL result=[self.theHelper insertData:[nsql UTF8String] tableName:TABLE_EVENT];
    if(result)
    {
        NSLog(@"%@,addSilent success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,addSilent fail.",DATAMANAGER_TAG);
    }
    return result;
}
//更新静音时段
-(BOOL)updateSilent:(Silent *)silent deviceUid:(NSString *)deviceUid
{
    NSLog(@"%@,updateSilent entered,deviceUid is %@.",DATAMANAGER_TAG,deviceUid);
    if([self checkValid]==NO)
        return NO;
    if(deviceUid==NULL||silent==NULL||!([deviceUid intValue]>0))
    {
        NSLog(@"%@,updateSilent,parameters is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"update event set type=%d,start_time=\"%@\",end_time=\"%@\",title=\"%@\",desc=\"%@\",cycle=\"%@\",is_active=%d where silent_id=%@ and deviceuid=%@;",silent.type,silent.startTime,silent.endTime,silent.title,silent.desc,silent.cycle,silent.active,silent.slient_id,deviceUid];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_EVENT];
    if(result)
    {
        NSLog(@"%@,updateSilent success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateSilent fail.",DATAMANAGER_TAG);
    }
    return result;
}
//删除静音时段
-(BOOL)deleteSilent:(NSString *)silentId deviceUid:(NSString *)deviceUid
{
    NSLog(@"%@,deleteSilent entered,silentId is %@,deviceUid is %@.",DATAMANAGER_TAG,silentId,deviceUid);
    if([self checkValid]==NO)
        return NO;
    if(silentId==NULL||!([silentId intValue]>0)||deviceUid==NULL)
    {
        NSLog(@"%@,deleteSilent,silentId is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"delete from event where silent_id=%@ and deviceuid=%@;",silentId,deviceUid];
    BOOL result=[self.theHelper deleteData:[nsql UTF8String] tableName:TABLE_EVENT];
    if(result)
    {
        NSLog(@"%@,deleteSilent success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,deleteSilent fail.",DATAMANAGER_TAG);
    }
    return result;
}
//获取更新静音时段
-(BOOL)updateSilents:(NSArray *)silents deviceUid:(NSString *)deviceUid
{
    NSLog(@"%@,updateSilents entered,deviceUid is %@.",DATAMANAGER_TAG,deviceUid);
    if([self checkValid]==NO)
        return NO;
    if(silents==NULL||[silents count]<=0||deviceUid==NULL)
    {
        NSLog(@"%@,updateSilents,there is no sql to exce.",DATAMANAGER_TAG);
        return NO;
    }
    NSMutableArray *sqlArray=[[NSMutableArray alloc] init];
    if(sqlArray==NULL)
    {
        NSLog(@"%@,updateSilents,sqlArray is null.",DATAMANAGER_TAG);
        return NO;
    }
    for(int i=0;i<[silents count];i++)
    {
        Silent *silent=[silents objectAtIndex:i];
        if(silent==NULL||silent.slient_id==NULL)
        {
            NSLog(@"%@,updateSilents,silent is null,index is %d.",DATAMANAGER_TAG,i);
            continue;
        }
        NSString *nsql1=[NSString stringWithFormat:@"select * from event where silent_id=%@ and deviceuid=%@;",silent.slient_id,deviceUid];
        NSArray *data1=[self.theHelper searchData:[nsql1 UTF8String]];
        NSString *nsql2=NULL;
        if(data1!=NULL&&[data1 count]>0)//already exist
        {
            if(silent.action==0)
            {
                nsql2=[NSString stringWithFormat:@"delete from event where silent_id=%@ and deviceuid=%@;",silent.slient_id,deviceUid];
            }
            else
            {
                nsql2=[NSString stringWithFormat:@"update event set type=%d,start_time=\"%@\",end_time=\"%@\",title=\"%@\",desc=\"%@\",cycle=\"%@\",is_active=%d,update_time=\"%@\" where silent_id=%@ and deviceuid=%@;",silent.type,silent.startTime,silent.endTime,silent.title,silent.desc,silent.cycle,silent.active,silent.updateTime,silent.slient_id,deviceUid];
            }
        }
        else
        {
            if(silent.action==1)
            {
                nsql2=[NSString stringWithFormat:@"insert into event (silent_id,deviceuid,type,start_time,end_time,title,desc,cycle,is_active,update_time) values (%@,%@,%d,\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",%d,\"%@\");",silent.slient_id,deviceUid,silent.type,silent.startTime,silent.endTime,silent.title,silent.desc,silent.cycle,silent.active,silent.updateTime];
            }
        }
        if(nsql2!=NULL)
        {
            [sqlArray addObject:nsql2];
        }
        nsql2=NULL;
    }
    if([sqlArray count]==0)
    {
        NSLog(@"%@,updateSilents,there is no sql to exec.",DATAMANAGER_TAG);
        return NO;
    }
    BOOL result=[self.theHelper execSql:sqlArray];
    if(result)
    {
        NSLog(@"%@,updateSilents success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateSilents fail.",DATAMANAGER_TAG);
    }
    return result;
}
-(BOOL)IsSameSilent:(NSString *)deviceUid silent:(Silent *)silent isNew:(BOOL)isNew
{
    if([self checkValid]==NO)
        return NO;
    if(deviceUid==NULL||!([deviceUid intValue]>0)||silent==NULL)
    {
        NSLog(@"%@,IsSameSilent,parameters is null.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=NULL;
    if(isNew)
        nsql=[NSString stringWithFormat:@"select * from event where deviceuid=%@ and start_time=\"%@\" and end_time=\"%@\";",deviceUid,silent.startTime,silent.endTime];
    else
        nsql=[NSString stringWithFormat:@"select * from event where deviceuid=%@ and start_time=\"%@\" and end_time=\"%@\" and silent_id<>%@;",deviceUid,silent.startTime,silent.endTime,silent.slient_id];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]<=0)
        return NO;
    else
        return YES;
}
-(NSString *)getManagerTS:(NSString *)deviceUid accountId:(NSString *)accountId
{
    NSLog(@"%@,getManagerTS entered,deviceUid is %@,accountId is %@.",DATAMANAGER_TAG,deviceUid,accountId);
    if([self checkValid]==NO)
        return NULL;
    if(deviceUid==NULL||!([deviceUid intValue]>0)||accountId==NULL||!([accountId intValue]>0))
    {
        NSLog(@"%@,getManagerTS,deviceUid is illegal.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select update_time from manager where deviceuid=%@  and account_id=%@ and (type=1 or type=2) order by update_time desc limit 1;",deviceUid,accountId];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getManagerTS,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSDictionary *info=[data objectAtIndex:0];
    if(info==NULL||[info count]==0)
    {
        NSLog(@"%@,getManagerTS,info is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *ts=[info valueForKey:@"update_time"];
    return ts;
    
}
-(NSString *)getGroupManagerTS:(NSString *)groupId accountId:(NSString *)accountId
{
    NSLog(@"%@,getGroupManagerTS entered,groupId is %@,accountId is %@.",DATAMANAGER_TAG,groupId,accountId);
    if([self checkValid]==NO)
        return NULL;
    if(groupId==NULL||!([groupId intValue]>0)||accountId==NULL||!([accountId intValue]>0))
    {
        NSLog(@"%@,getGroupManagerTS,deviceUid is illegal.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select update_time from manager where group_id=%@  and account_id=%@ and (type=1 or type=2) order by update_time desc limit 1;",groupId,accountId];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getGroupManagerTS,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSDictionary *info=[data objectAtIndex:0];
    if(info==NULL||[info count]==0)
    {
        NSLog(@"%@,getGroupManagerTS,info is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *ts=[info valueForKey:@"update_time"];
    return ts;
    
}
-(NSString *)getContactTS:(NSString *)deviceUid accountId:(NSString *)accountId
{
    NSLog(@"%@,getContactTS entered,deviceUid is %@,accountId is %@.",DATAMANAGER_TAG,deviceUid,accountId);
    if([self checkValid]==NO)
        return NULL;
    if(deviceUid==NULL||!([deviceUid intValue]>0)||accountId==NULL||!([accountId intValue]>0))
    {
        NSLog(@"%@,getContactTS,deviceUid is illegal.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select update_time from manager where deviceuid=%@  and account_id=%@ and (type=3 or type=4) order by update_time desc limit 1;",deviceUid,accountId];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getContactTS,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSDictionary *info=[data objectAtIndex:0];
    if(info==NULL||[info count]==0)
    {
        NSLog(@"%@,getContactTS,info is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *ts=[info valueForKey:@"update_time"];
    return ts;
    
}
-(NSString *)getFenceTS:(NSString *)deviceUid
{
    NSLog(@"%@,getFenceTS entered,deviceUid is %@.",DATAMANAGER_TAG,deviceUid);
    if([self checkValid]==NO)
        return NULL;
    if(deviceUid==NULL||!([deviceUid intValue]>0))
    {
        NSLog(@"%@,getFenceTS,deviceUid is illegal.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select update_time from fence where deviceuid=%@ order by update_time desc limit 1;",deviceUid];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getFenceTS,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSDictionary *info=[data objectAtIndex:0];
    if(info==NULL||[info count]==0)
    {
        NSLog(@"%@,getFenceTS,info is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *ts=[info valueForKey:@"update_time"];
    return ts;

}
-(NSString *)getRemindTs:(NSString *)deviceUid
{
    NSLog(@"%@,getRemindTs entered,deviceUid is %@.",DATAMANAGER_TAG,deviceUid);
    if([self checkValid]==NO)
        return NULL;
    if(deviceUid==NULL||!([deviceUid intValue]>0))
    {
        NSLog(@"%@,getRemindTs,deviceUid is illegal.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select update_time from remind where deviceuid=%@ order by update_time desc limit 1;",deviceUid];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getRemindTs,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSDictionary *info=[data objectAtIndex:0];
    if(info==NULL||[info count]==0)
    {
        NSLog(@"%@,getRemindTs,info is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *ts=[info valueForKey:@"update_time"];
    return ts;
}
//读取photoHash
-(NSMutableDictionary *)getWatchPhotoHash
{
    NSLog(@"%@,getWatchPhotoHash entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NULL;
    NSString *nsql=[NSString stringWithFormat:@"select photohash,deviceuid from watch;"];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getWatchPhotoHash,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSMutableDictionary *photoHashList=[[NSMutableDictionary alloc] init];
    for(int i=0;i<[data count];i++)
    {
        NSDictionary *info=[data objectAtIndex:i];
        if(info==NULL||[info count]==0)
        {
            NSLog(@"%@,getWatchPhotoHash,info is null.",DATAMANAGER_TAG);
            continue;
        }
        
        if([info valueForKey:@"photohash"]!=NULL&&[info valueForKey:@"deviceuid"]!=NULL)
        {
            [photoHashList setValue:[info valueForKey:@"photohash"] forKey:[info valueForKey:@"deviceuid"]];
        }
    }
    return photoHashList;
}
 //读取管理人员的photoHash
-(NSMutableDictionary *)getManagerPhotoHash:(NSString *)deviceUid
{
    NSLog(@"%@,getManagerPhotoHash entered,deviceUid is %@.",DATAMANAGER_TAG,deviceUid);
    if([self checkValid]==NO)
        return NULL;
    if(deviceUid==NULL||![deviceUid intValue]>0)
    {
        NSLog(@"%@,getManagerPhotoHash,deviceUid is illegal.",DATAMANAGER_TAG);
        return NULL;
    }
    User *user=[User sharedSingleton];
    if(user==NULL)
        return NULL;
    NSString *nsql=[NSString stringWithFormat:@"select photohash,user_id from manager where deviceuid=%@ and account_id=%@ and (type=%d or type=%d);",deviceUid,user.uid,ADMIN,VICE_ADMIN];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getManagerPhotoHash,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSMutableDictionary *photoHashList=[[NSMutableDictionary alloc] init];
    for(int i=0;i<[data count];i++)
    {
        NSDictionary *info=[data objectAtIndex:i];
        if(info==NULL||[info count]==0)
        {
            NSLog(@"%@,getManagerPhotoHash,info is null.",DATAMANAGER_TAG);
            continue;
        }
        
        if([info valueForKey:@"photohash"]!=NULL&&[info valueForKey:@"user_id"]!=NULL)
        {
            [photoHashList setValue:[info valueForKey:@"photohash"] forKey:[info valueForKey:@"user_id"]];
        }
    }
    return photoHashList;
}
//读取联系人的photoHash
-(NSMutableDictionary *)getContactPhotoHash:(NSString *)deviceUid
{
    NSLog(@"%@,getContactPhotoHash entered,deviceUid is %@.",DATAMANAGER_TAG,deviceUid);
    if([self checkValid]==NO)
        return NULL;
    if(deviceUid==NULL||![deviceUid intValue]>0)
    {
        NSLog(@"%@,getContactPhotoHash,deviceUid is illegal.",DATAMANAGER_TAG);
        return NULL;
    }
    User *user=[User sharedSingleton];
    if(user==NULL)
        return NULL;
    NSString *nsql=[NSString stringWithFormat:@"select photohash,user_id from manager where deviceuid=%@ and account_id=%@ and (type=%d or type=%d);",deviceUid,user.uid,CONTACT,SOS_CONTACT];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getContactPhotoHash,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSMutableDictionary *photoHashList=[[NSMutableDictionary alloc] init];
    for(int i=0;i<[data count];i++)
    {
        NSDictionary *info=[data objectAtIndex:i];
        if(info==NULL||[info count]==0)
        {
            NSLog(@"%@,getContactPhotoHash,info is null.",DATAMANAGER_TAG);
            continue;
        }
        
        if([info valueForKey:@"photohash"]!=NULL&&[info valueForKey:@"user_id"]!=NULL)
        {
            [photoHashList setValue:[info valueForKey:@"photohash"] forKey:[info valueForKey:@"user_id"]];
        }
    }
    return photoHashList;
}

-(BOOL)addReminder:(Reminder *)reminder deviceUid:(NSString *)deviceUid
{
    NSLog(@"%@,addReminder entered,deviceUid is %@.",DATAMANAGER_TAG,deviceUid);
    if([self checkValid]==NO)
        return NO;
    if(reminder==NULL||reminder.reminder_id==NULL||deviceUid==NULL||!([deviceUid intValue]>0))
    {
        NSLog(@"%@,addReminder,parameters is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"insert into remind(remind_id,deviceuid,type,remind_time,title,desc,cycle,is_active,audio_url,audio_hash,update_time,audio_time) values (%@,%@,%d,\"%@\",\"%@\",\"%@\",\"%@\",%d,\"%@\",\"%@\",\"%@\",%d);",reminder.reminder_id,deviceUid,reminder.type,reminder.remindTime,reminder.title,reminder.desc,reminder.cycle,reminder.is_active,reminder.audioUrl,reminder.audioHash,reminder.updateTime,reminder.autionTime];
    BOOL result=[self.theHelper insertData:[nsql UTF8String] tableName:TABLE_REMIND];
    if(result)
    {
        NSLog(@"%@,addReminder success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,addReminder fail.",DATAMANAGER_TAG);
    }
    return result;
    
}
-(BOOL)updateReminder:(Reminder *)reminder deviceUid:(NSString *)deviceUid
{
    NSLog(@"%@,updateReminder entered,deviceUid is %@.",DATAMANAGER_TAG,deviceUid);
    if([self checkValid]==NO)
        return NO;
    if(reminder==NULL||reminder.reminder_id==NULL||deviceUid==NULL||!([deviceUid intValue]>0))
    {
        NSLog(@"%@,updateReminder,parameters is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"update remind set type=%d,remind_time=\"%@\",title=\"%@\",desc=\"%@\",cycle=\"%@\",is_active=%d,audio_url=\"%@\",audio_hash=\"%@\",audio_time=%d where remind_id=%@ and deviceuid=%@;",reminder.type,reminder.remindTime,reminder.title,reminder.desc,reminder.cycle,reminder.is_active,reminder.audioUrl,reminder.audioHash,reminder.autionTime,reminder.reminder_id,deviceUid];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_REMIND];
    if(result)
    {
        NSLog(@"%@,updateReminder success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateReminder fail.",DATAMANAGER_TAG);
    }
    return result;
}
-(BOOL)deleteReminder:(NSString *)reminderId deviceUid:(NSString *)deviceUid
{
    NSLog(@"%@,deleteReminder entered,deviceUid is %@,reminderId is %@.",DATAMANAGER_TAG,deviceUid,reminderId);
    if([self checkValid]==NO)
        return NO;
    if(reminderId==NULL||!([reminderId intValue]>0)||deviceUid==NULL||!([deviceUid intValue]>0))
    {
        NSLog(@"%@,deleteReminder,parameters is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"delete from remind where remind_id=%@ and deviceuid=%@;",reminderId,deviceUid];
    BOOL result=[self.theHelper deleteData:[nsql UTF8String] tableName:TABLE_REMIND];
    if(result)
    {
        NSLog(@"%@,deleteReminder success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,deleteReminder fail.",DATAMANAGER_TAG);
    }
    return result;
}
-(BOOL)updateReminds:(NSArray *)reminders devieUid:(NSString *)deviceUid
{
    NSLog(@"%@,updateReminds entered,deviceUid is %@.",DATAMANAGER_TAG,deviceUid);
    if([self checkValid]==NO)
        return NO;
    if(reminders==NULL||[reminders count]<=0||deviceUid==NULL||!([deviceUid intValue]>0))
    {
        NSLog(@"%@,updateReminds,parameters is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    NSMutableArray *sqlArray=[[NSMutableArray alloc] init];
    if(sqlArray==NULL)
    {
        NSLog(@"%@,updateReminds,sqlArray is null,",DATAMANAGER_TAG);
        return NO;
    }
    for(int i=0;i<[reminders count];i++)
    {
        Reminder *reminder=[reminders objectAtIndex:i];
        if(reminder==NULL||reminder.reminder_id==NULL)
        {
            NSLog(@"%@,updateReminds,reminder is null.",DATAMANAGER_TAG);
            continue;
        }
        NSString *nsql1=[NSString stringWithFormat:@"select * from remind where remind_id=%@ and deviceuid=%@;",reminder.reminder_id,deviceUid];
        NSArray *data1=[self.theHelper searchData:[nsql1 UTF8String]];
        NSString *nsql2=NULL;
        if(data1!=NULL&&[data1 count]>0)//already exist
        {
            if(reminder.action==0)
            {
                nsql2=[NSString stringWithFormat:@"delete from remind where remind_id=%@ and deviceuid=%@;",reminder.reminder_id,deviceUid];
            }
            else
            {
                nsql2=[NSString stringWithFormat:@"update remind set type=%d,remind_time=\"%@\",title=\"%@\",desc=\"%@\",cycle=\"%@\",is_active=%d,update_time=\"%@\" where remind_id=%@ and deviceuid=%@;",reminder.type,reminder.remindTime,reminder.title,reminder.desc,reminder.cycle,reminder.is_active,reminder.updateTime,reminder.reminder_id,deviceUid];
            }
        }
        else
        {
            if(reminder.action==1)
            {
                nsql2=[NSString stringWithFormat:@"insert into remind(remind_id,deviceuid,type,remind_time,title,desc,cycle,is_active,update_time) values (%@,%@,%d,\"%@\",\"%@\",\"%@\",\"%@\",%d,\"%@\");",reminder.reminder_id,deviceUid,reminder.type,reminder.remindTime,reminder.title,reminder.desc,reminder.cycle,reminder.is_active,reminder.updateTime];
            }
        }
        if(nsql2!=NULL)
        {
           [sqlArray addObject:nsql2];
        }
        nsql2=NULL;
    }
    if([sqlArray count]==0)
    {
        NSLog(@"%@,updateReminds,there is no sql to exec.",DATAMANAGER_TAG);
        return NO;
    }
    BOOL result=[self.theHelper execSql:sqlArray];
    if(result)
    {
        NSLog(@"%@,updateReminds success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateReminds fail.",DATAMANAGER_TAG);
    }
    return result;
}
//读取提醒的audioHash
-(NSMutableDictionary *)getRemindAudioHash:(NSString *)deviceUid
{
    NSLog(@"%@,getRemindAudioHash entered,deviceUid is %@.",DATAMANAGER_TAG,deviceUid);
    if([self checkValid]==NO)
        return NULL;
    if(deviceUid==NULL||![deviceUid intValue]>0)
    {
        NSLog(@"%@,getRemindAudioHash,deviceUid is illegal.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select remind_id,audio_hash from remind where deviceuid=%@;",deviceUid];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getRemindAudioHash,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSMutableDictionary *audioHashList=[[NSMutableDictionary alloc] init];
    for(int i=0;i<[data count];i++)
    {
        NSDictionary *info=[data objectAtIndex:i];
        if(info==NULL||[info count]==0)
        {
            NSLog(@"%@,getRemindAudioHash,info is null.",DATAMANAGER_TAG);
            continue;
        }
        
        if([info valueForKey:@"audio_hash"]!=NULL&&[info valueForKey:@"remind_id"]!=NULL)
        {
            [audioHashList setValue:[info valueForKey:@"audio_hash"] forKey:[info valueForKey:@"remind_id"]];
        }
    }
    return audioHashList;
}
-(BOOL)updateRemindFile:(NSString *)reminderId filePath:(NSString *)filePath audioHash:(NSString *)audioHash
{
    NSLog(@"%@,updateRemindFile entered,reminderId is %@,filePath is %@,audioHash is %@.",DATAMANAGER_TAG,reminderId,filePath,audioHash);
    if([self checkValid]==NO)
        return NO;
    if(reminderId==NULL||filePath==NULL)
    {
        NSLog(@"%@,updateRemindFile,parameters is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"update remind set audio_url=\"%@\",audio_hash=\"%@\" where remind_id=%@;",filePath,audioHash,reminderId];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_WATCH];
    if(result)
    {
        NSLog(@"%@,updateRemindFile success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateRemindFile fail.",DATAMANAGER_TAG);
    }
    return result;
}


/*******************
 Setting相关
 ********************/
-(BOOL)updateSetting:(Setting *)setting deviceUid:(NSString *)deviceUid
{
    NSLog(@"%@,updateSetting entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NO;
    if(setting==NULL)
    {
        NSLog(@"%@,updateSetting,parameter is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=NULL;
    if(setting.type==MESSAGE_PUSH)
    {
        nsql=[NSString stringWithFormat:@"update watch set message_push=%d,message_time=\"%@\" where deviceuid=%@;",setting.value,setting.pushTime,deviceUid];
    }
    //UI更改
    else if (setting.type == SHORTNUM)
    {
        nsql=[NSString stringWithFormat:@"update watch set shortNum=%d where deviceuid=%@;",setting.value,deviceUid];
    }
    else
    {
        nsql=[NSString stringWithFormat:@"update watch set task_push=%d,task_time=\"%@\" where deviceuid=%@;",setting.value,setting.pushTime,deviceUid];
    }
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_WATCH];
    if(result)
    {
        NSLog(@"%@,updateSetting success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateSetting fail.",DATAMANAGER_TAG);
    }
    return result;

    
}
-(BOOL)updateSettingArray:(NSArray *)settingArray deviceUid:(NSString *)deviceUid
{
    NSLog(@"%@,updateSettingArray entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NO;
    if(settingArray==NULL||[settingArray count]==0||deviceUid==NULL||!([deviceUid intValue]>0))
    {
        NSLog(@"%@,updateSettingArray,parameter is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    NSMutableArray *sqlArray=[[NSMutableArray alloc] init];
    if(sqlArray==NULL)
    {
        NSLog(@"%@,updateSettingArray,sqlArray is null.",DATAMANAGER_TAG);
        return NO;
    }
    for(int i=0;i<[settingArray count];i++)
    {
        Setting *theset=[settingArray objectAtIndex:i];
        if(theset==NULL)
        {
            NSLog(@"%@,updateSettingArry,theset is null.",DATAMANAGER_TAG);
            continue;
        }
        NSString *nsql=NULL;
        if(theset.type==MESSAGE_PUSH)
        {
            nsql=[NSString stringWithFormat:@"update watch set message_push=%d,message_time=\"%@\" where deviceuid=%@;",theset.value,theset.pushTime,deviceUid];
        }
        //UI更改
        else if (theset.type == SHORTNUM)
        {
            nsql=[NSString stringWithFormat:@"update watch set shortNum=%d where deviceuid=%@;",theset.value,deviceUid];
        }
        else
        {
            nsql=[NSString stringWithFormat:@"update watch set task_push=%d,task_time=\"%@\" where deviceuid=%@;",theset.value,theset.pushTime,deviceUid];
        }
        if(nsql!=NULL)
        {
            [sqlArray addObject:nsql];
            nsql=NULL;
        }
       
    }
    if(sqlArray==NULL||[sqlArray count]==0)
    {
        NSLog(@"%@,updateSettingArray,there is no sql to exec.",DATAMANAGER_TAG);
        return NO;
    }
    BOOL result=[self.theHelper execSql:sqlArray];
    if(result)
    {
        NSLog(@"%@,updateSettingArray success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateSettingArray fail.",DATAMANAGER_TAG);
    }
    return result;
}
-(NSArray *)getMonthLocData:(int)year month:(int)month deviceUid:(NSString *)deviceUid
{
    NSLog(@"%@,getMonthLocData entered,year is %d,month is %d,deviceUid is %@.",DATAMANAGER_TAG,year,month,deviceUid);
    if([self checkValid]==NO)
        return NULL;
    if(year<=0||month<0||month>31||deviceUid==NULL)
    {
        NSLog(@"%@,getMonthLocData,parameters is illegal.",DATAMANAGER_TAG);
        return NULL;
    }
    NSMutableArray *monthLoc=[[NSMutableArray alloc] initWithCapacity:32];
    for(int i=0;i<32;i++)
    {
        [monthLoc addObject:@"0"];
    }
    NSString *nsql=[NSString stringWithFormat:@"select distinct day from position where year=%d and month=%d and deviceuid=%@;",year,month,deviceUid];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
        return monthLoc;
    for (int i=0; i<[data count]; i++) {
        NSDictionary *dayInfo=[data objectAtIndex:i];
        if(dayInfo==NULL||[dayInfo count]==0)
            continue;
        NSString *dayStr=[dayInfo valueForKey:@"day"];
        if(dayStr==NULL)
            continue;
        int day=[dayStr intValue];
        if(day<0||day>31)
            continue;
        [monthLoc replaceObjectAtIndex:day withObject:@"1"];
    }
    return monthLoc;
}

-(BOOL)logoutUser:(User *)user
{
    NSLog(@"%@,logoutUser entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NO;
    if(user==NULL||user.uid==NULL)
    {
        NSLog(@"%@,user is null.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"update account set is_active=0 where user_id=%@;",user.uid];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_USER];
    if(result)
    {
        NSLog(@"%@,lououtUser success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,logoutUser fail.",DATAMANAGER_TAG);
    }
    return result;
}
-(BOOL)logoutOtherAccounts:(User *)user
{
    NSLog(@"%@,logoutOtherAccounts entered.",DATAMANAGER_TAG);
    if([self checkValid]==NO)
        return NO;
    if(user==NULL||user.uid==NULL)
    {
        NSLog(@"%@,logoutOtherAccounts,user is null.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"update account set is_active=0 where user_id<>%@;",user.uid];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_USER];
    if(result)
    {
        NSLog(@"%@,logoutOtherAccounts success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,logoutOtherAccounts fail.",DATAMANAGER_TAG);
    }
    return result;
}
-(NSString *)getLatestSigleChatTime:(NSString *)targetUid accountId:(NSString *)accountId
{
    if([self checkValid]==NO)
        return NULL;
    if(targetUid==NULL||accountId==NULL)
    {
        NSLog(@"%@,getLatestSigleChatTime,parameter is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select gen_time from chart where (targetuid=%@ and user_id=%@) or (targetuid=%@ and user_id=%@) and account_id=%@ order by gen_time desc limit 1;",accountId,targetUid,targetUid,accountId,accountId];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
        return NULL;
    NSDictionary *dataInfo=[data objectAtIndex:0];
    if(dataInfo==NULL||[dataInfo count]==0)
        return NULL;
    NSString *time=[dataInfo valueForKey:@"gen_time"];
    if(time==NULL||[time isEqualToString:@"(null)"]||[time isEqualToString:@""])
        return NULL;
    return time;
}
-(NSString *)getLatestGroupChatTime:(int)groupId accountId:(NSString *)accountId
{
    if([self checkValid]==NO)
        return NULL;
    if(groupId<=0||accountId==NULL)
    {
        NSLog(@"%@,getLatestGroupChatTime,parameter is illegal.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select gen_time from chart where group_id=%d and ((user_id in (select deviceuid from watch where  group_id=%d and  account_id=%@)) or (user_id in (select user_id from manager where group_id=%d and account_id=%@ and (type=%d or type=%d)  group by user_id)))  and account_id=%@  and is_finish=1 order by gen_time desc limit 1;",groupId,groupId,accountId,groupId,accountId,ADMIN,VICE_ADMIN,accountId];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
        return NULL;
    NSDictionary *dataInfo=[data objectAtIndex:0];
    if(dataInfo==NULL||[dataInfo count]==0)
        return NULL;
    NSString *time=[dataInfo valueForKey:@"gen_time"];
    if(time==NULL||[time isEqualToString:@"(null)"]||[time isEqualToString:@""])
        return NULL;
    return time;
}
-(BOOL)updateRelation:(NSString *)deviceUid managerUid:(NSString *)managerUid alias:(NSString *)alias
{
    if([self checkValid]==NO)
        return NO;
    if(deviceUid==NULL||managerUid==NULL||alias==NULL)
    {
        NSLog(@"%@,updateRelation,parameter is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    User *user=[User sharedSingleton];
    if(user==NULL)
        return NO;
    NSString *nsql=[NSString stringWithFormat:@"update manager set alias=\"%@\" where deviceuid=%@ and user_id=%@ and account_id=%@;",alias,deviceUid,managerUid,user.uid];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_MANAGER];
    if(result)
    {
        NSLog(@"%@,updateRelation success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateRelation fail.",DATAMANAGER_TAG);
    }
    return result;
}
-(int)getUnReadNums:(NSString *)deviceUid groupId:(int)groupId accountId:(NSString *)accountId
{
    if([self checkValid]==NO)
        return 0;
    if(deviceUid==NULL||accountId==NULL)
    {
        NSLog(@"%@,getUnReadNums,parameters is illegal.",DATAMANAGER_TAG);
        return 0;
    }
    User *user=[User sharedSingleton];
    if(user==NULL||user.uid==NULL)
        return 0;
    NSString *nsql=[NSString stringWithFormat:@"select count(1) as nums from chart where     account_id=%@ and is_view=0 and is_finish=1 and ((group_id=%d and ((user_id in (select deviceuid from watch where  group_id=%d and  account_id=%@))  or (user_id in (select user_id from manager where group_id=%d and account_id=%@ and (type=%d or type=%d) and user_id<>%@ and deviceuid=%@ group by user_id)) )) or ((user_id in (select deviceuid from watch where  group_id=%d and  account_id=%@))  and targetuid=%@) or ((user_id in (select user_id from manager where group_id=%d and account_id=%@ and (type=%d or type=%d) and user_id<>%@ and deviceuid=%@ group by user_id))  and targetuid=%@));",accountId,groupId,groupId,accountId,groupId,accountId,ADMIN,VICE_ADMIN,user.uid,deviceUid,groupId,accountId,user.uid,groupId,accountId,ADMIN,VICE_ADMIN,user.uid,deviceUid,user.uid];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
        return 0;
    NSDictionary *dataInfo=[data objectAtIndex:0];
    if(dataInfo==NULL||[dataInfo count]==0)
        return 0;
    if([dataInfo valueForKey:@"nums"]==NULL)
        return 0;
    return [[dataInfo valueForKey:@"nums"] intValue];
}
-(NSArray *)getGroupLists:(NSString *)accountId
{
    if([self checkValid]==NO)
        return 0;
    if(accountId==NULL)
    {
        NSLog(@"%@,getGroupLists,parameters is illegal.",DATAMANAGER_TAG);
        return 0;
    }
    NSString *nsql=[NSString stringWithFormat:@"select distinct group_id from watch where account_id=%@;",accountId];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
        return NULL;
    NSMutableArray *groupList=[[NSMutableArray alloc] init];
    if(groupList==NULL)
        return NULL;
    for(int i=0;i<[data count];i++)
    {
        NSDictionary *dataInfo=[data objectAtIndex:i];
        if(dataInfo==NULL||[dataInfo count]==0)
            continue;
        NSString *groupId=[dataInfo valueForKey:@"group_id"];
        if(groupId==NULL||[groupId intValue]<=0)
            continue;
        [groupList addObject:groupId];
        groupId=NULL;
    }
    return groupList;
}
//读取家庭圈所有副管理员，得到不同的user_id,群组策略修改
-(NSArray *)getDiffGroupManagers:(NSString *)groupId accountId:(NSString *)accountId
{
    NSLog(@"%@,getDiffGroupManagers entered,groupId is %@,accountId is %@.",DATAMANAGER_TAG,groupId,accountId);
    if([self checkValid]==NO)
        return NULL;
    if(groupId==NULL|| [groupId intValue]<=0||accountId==NULL||!([accountId intValue]>0))
    {
        NSLog(@"%@,getDiffGroupManagers, parameters is illegal.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from manager where group_id=%@ and account_id=%@ and type=%d group by user_id;",groupId,accountId,VICE_ADMIN];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getDiffGroupManagers,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSMutableArray *managerList=[[NSMutableArray alloc] init];
    if(managerList==NULL)
    {
        NSLog(@"%@,getDiffGroupManagers,managerList is null.",DATAMANAGER_TAG);
        return NULL;
    }
    for(int i=0;i<[data count];i++)
    {
        NSDictionary *managerInfo=[data objectAtIndex:i];
        if(managerInfo==NULL||[managerInfo count]==0)
        {
            NSLog(@"%@,getDiffGroupManagers,managerInfo is null,index is %d.",DATAMANAGER_TAG,i);
            continue;
        }
        Manager *manager=[[Manager alloc] init];
        manager.manager_id=[managerInfo valueForKey:@"id"];
        manager.user_id=[managerInfo valueForKey:@"user_id"];
        manager.alias=[managerInfo valueForKey:@"alias"];
        manager.pic_url=[managerInfo valueForKey:@"pic_url"];
        manager.birth=[managerInfo valueForKey:@"birth"];
        manager.phone=[managerInfo valueForKey:@"phone"];
        manager.type=[Tooler getManagerType:[[managerInfo valueForKey:@"type"] intValue]];
        manager.nickName=[managerInfo valueForKey:@"nick_name"];
        manager.deviceUid=[managerInfo valueForKey:@"deviceuid"];
        [managerList addObject:manager];
        manager=NULL;
    }
    return managerList;
}
-(BOOL)updateViceAdminInfo:(Manager *)viceAdmin deviceUid:(NSString *)deviceUid
{
    if([self checkValid]==NO)
        return NO;
    if(viceAdmin==NULL||viceAdmin.user_id==NULL||deviceUid==NULL)
    {
        NSLog(@"%@,updateViceAdminInfo,parameters is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    User *user=[User sharedSingleton];
    if(user==NULL)
        return NO;
    NSString *nsql=[NSString stringWithFormat:@"update manager set is_sos=%d where user_id=%@ and deviceuid=%@ and account_id=%@;",viceAdmin.isSos,viceAdmin.user_id,deviceUid,user.uid];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_MANAGER];
    if(result)
    {
        NSLog(@"%@,updateViceAdminInfo success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateViceAdminInfo fail.",DATAMANAGER_TAG);
    }
    return result;
}
-(BOOL)setChatRead:(NSString *)chatId
{
    if([self checkValid]==NO)
        return NO;
    if(chatId==NULL||!([chatId intValue]>0))
    {
        NSLog(@"%@,setChatRead,parameters is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"update chart set is_read=1 where id=%@  ;",chatId];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_CHART];
    if(result)
    {
        NSLog(@"%@,setChatRead success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@.setChatRead fail.",DATAMANAGER_TAG);
    }
    return result;
}
-(BOOL)setSingleChatView:(NSString *)senderUid
{
    if([self checkValid]==NO)
        return NO;
    if(senderUid==NULL||!([senderUid intValue]>0))
    {
        NSLog(@"%@,setSingleChatView,parameters is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    User *user=[User sharedSingleton];
    if(user==NULL||user.uid==NULL)
        return NO;
    NSString *nsql=[NSString stringWithFormat:@"update chart set is_view=1 where user_id=%@ and targetuid=%@ and account_id=%@;",senderUid,user.uid,user.uid];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_CHART];
    if(result)
    {
        NSLog(@"%@,setSingleChatView success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@.setSingleChatView fail.",DATAMANAGER_TAG);
    }
    return result;
}
-(BOOL)setGroupChatView:(int)groupId
{
    if([self checkValid]==NO)
        return NO;
    if(groupId<=0)
    {
        NSLog(@"%@,setGroupChatView,parameters is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    User *user=[User sharedSingleton];
    if(user==NULL||user.uid==NULL)
        return NO;
    NSString *nsql=[NSString stringWithFormat:@"update chart set is_view=1 where group_id=%d and account_id=%@;",groupId,user.uid];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_CHART];
    if(result)
    {
        NSLog(@"%@,setGroupChatView success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@.setGroupChatView fail.",DATAMANAGER_TAG);
    }
    return result;
}

-(BOOL)isChatRead:(NSString *)chatId
{
    if([self checkValid]==NO)
        return NO;
    if(chatId==NULL||!([chatId intValue]>0))
    {
        NSLog(@"%@,isChatRead,parameters is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"select is_read from chart where id=%@",chatId];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
        return NO;
    NSDictionary *readInfo=[data objectAtIndex:0];
    if(readInfo==NULL||[readInfo count]==0)
        return NO;
    int isRead=[[readInfo valueForKey:@"is_read"] intValue];
    return isRead;
    
    
}
-(BOOL)isChatSend:(NSString *)chatId
{
    if([self checkValid]==NO)
        return NO;
    if(chatId==NULL||!([chatId intValue]>0))
    {
        NSLog(@"%@,isChatSend,parameters is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"select is_send from chart where id=%@",chatId];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
        return NO;
    NSDictionary *sendInfo=[data objectAtIndex:0];
    if(sendInfo==NULL||[sendInfo count]==0)
        return NO;
    int isSead=[[sendInfo valueForKey:@"is_send"] intValue];
    return isSead;
    
    
}

-(BOOL)setWatchTheme:(int)watchTheme deviceUid:(NSString *)deviceUid
{
    if([self checkValid]==NO)
        return NO;
    if(watchTheme<0||watchTheme>15||deviceUid==NULL)
    {
        NSLog(@"%@,setWatchTheme,parameters is illegal.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"update watch set watch_theme=%d where deviceuid=%@;",watchTheme,deviceUid];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_WATCH];
    if(result)
    {
        NSLog(@"%@,setWatchTheme success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,setWatchTheme fail.",DATAMANAGER_TAG);
    }
    return result;
}
-(NSArray *)getSendFailChats:(NSString *)accountId
{
    if([self checkValid]==NO)
        return NULL;
    if(accountId==NULL)
    {
        NSLog(@"%@,getSendFailChats,parameters is illegal.",DATAMANAGER_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from chart where account_id=%@ and  is_send=0 and user_id=%@;",accountId,accountId];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getSendFailChats,data is null.",DATAMANAGER_TAG);
        return NULL;
    }
    NSMutableArray *chatList=[[NSMutableArray alloc] init];
    if(chatList==NULL)
    {
        NSLog(@"%@,getSendFailChats,chatList is null.",DATAMANAGER_TAG);
        return NULL;
    }
    for (int i=0; i<[data count]; i++) {
        NSDictionary *info=[data objectAtIndex:i];
        if(info==NULL||[info count]==0)
        {
            NSLog(@"%@,getSendFailChats,info is null.",DATAMANAGER_TAG);
            continue;
        }
        Chat *chat=[[Chat alloc] init];
        chat.user_id=[info valueForKey:@"user_id"];
        chat.file_url=[info valueForKey:@"file_url"];
        chat.duration=[[info valueForKey:@"duration"] intValue];
        chat.genTime=[info valueForKey:@"gen_time"];//转换成日期格式
        chat.groupId=[[info valueForKey:@"group_id"] intValue];
        chat.is_read=[[info valueForKey:@"is_read"] intValue];
        chat.is_send=[[info valueForKey:@"is_send"] intValue];
        chat.chatId=[info valueForKey:@"id"];
        chat.type=[[info valueForKey:@"type"] intValue];
        chat.amr_url=[info valueForKey:@"amr_url"];
        chat.sqeId=[info valueForKey:@"seqid"];
        chat.targetUid=[info valueForKey:@"targetuid"];
        [chatList addObject:chat];
        chat=NULL;
    }
    return chatList;

}
-(BOOL)IsSameContact:(NSString *)deviceUid contact:(Contact *)contact isNew:(BOOL)isNew
{
    if([self checkValid]==NO)
        return NO;
    if(deviceUid==NULL||!([deviceUid intValue]>0)||contact==NULL)
    {
        NSLog(@"%@,IsSameContact,parameters is null.",DATAMANAGER_TAG);
        return NO;
    }
    User *user=[User sharedSingleton];
    if(user==NULL||user.uid==NULL||!([user.uid intValue]>0))
    {
        NSLog(@"%@,IsSameContact,user is null.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=NULL;
    if(isNew)
        nsql=[NSString stringWithFormat:@"select * from manager where deviceuid=%@  and phone=\"%@\" and (type=%d or type=%d) and account_id=%@;",deviceUid,contact.phoneNum,CONTACT,SOS_CONTACT,user.uid];
    else
        nsql=[NSString stringWithFormat:@"select * from manager where deviceuid=%@  and phone=\"%@\" and (type=%d or type=%d) and account_id=%@ and user_id<>%@;",deviceUid,contact.phoneNum,CONTACT,SOS_CONTACT,user.uid,contact.user_id];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]<=0)
        return NO;
    else
        return YES;
}
-(BOOL)IsSameFence:(Fence *)fence isNew:(BOOL)isNew
{
    if([self checkValid]==NO)
        return NO;
    if(fence==NULL)
    {
        NSLog(@"%@,IsSameFence,parameters is null.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=NULL;
    if(isNew)
        nsql=[NSString stringWithFormat:@"select * from fence where deviceuid=%@   and  description=\"%@\" and radius=%f;",fence.deviceUid,fence.desc,fence.radius];
    else
        nsql=[NSString stringWithFormat:@"select * from fence where deviceuid=%@   and  description=\"%@\" and radius=%f and fence_id<>%@;",fence.deviceUid,fence.desc,fence.radius,fence.fence_id];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]<=0)
        return NO;
    else
        return YES;
}
-(BOOL)isWatch:(NSString *)targetUid
{
    if([self checkValid]==NO)
        return NO;
    if(targetUid==NULL)
    {
        NSLog(@"%@,isWatch,parameters is null.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from watch where deviceuid=%@;",targetUid];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
        return NO;
    return YES;
}
-(int)todayAssignTaskNum:(NSString *)deviceUid
{
    if([self checkValid]==NO)
        return 0;
    if(deviceUid==NULL)
    {
        NSLog(@"%@,todayAssignTaskNum,parameters is null.",DATAMANAGER_TAG);
        return 0;
    }
    long startTs=[Tooler getTodayStartTs];
    NSString *nsql=[NSString stringWithFormat:@"select * from task where target_uid=%@  and action=%d and create_time>=%ld  ;",deviceUid,CONFIRM,startTs];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
        return 0;
    int taskNums=(int)[data count];
    return taskNums;
}
-(BOOL)updateWatchVersion:(Version *)version
{
    if([self checkValid]==NO)
        return NO;
    if(version==NULL||version.deviceUid==NULL||version.fwVer==NULL)
    {
        NSLog(@"%@,updateWatchVersion,parameters is null.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"update watch set version=\"%@\",is_latest=1 where deviceuid=%@;",version.fwVer,version.deviceUid];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_WATCH];
    if(result)
    {
        NSLog(@"%@,updateWatchVersion success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateWatchVersion fail.",DATAMANAGER_TAG);
    }
    return result;
}
-(BOOL)updateVersionState:(NSString *)deviceUid
{
    if([self checkValid]==NO)
        return NO;
    if(deviceUid==NULL)
    {
        NSLog(@"%@,updateVersionState,parameters is null.",DATAMANAGER_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"update watch set is_latest=1 where deviceuid=%@;",deviceUid];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_WATCH];
    if(result)
    {
        NSLog(@"%@,updateVersionState success.",DATAMANAGER_TAG);
    }
    else
    {
        NSLog(@"%@,updateVersionState fail.",DATAMANAGER_TAG);

    }
    return result;
}
-(NSArray *)getFailWhatchPhotoHash
{
    if([self checkValid]==NO)
        return NULL;
    User *user=[User sharedSingleton];
    if(user==NULL||user.uid==NULL)
        return NULL;
    NSString *nsql=[NSString stringWithFormat:@"select photohash,deviceuid from watch where account_id=%@ and (pic_url=null or pic_url=\"(null)\" or pic_url=\"\") and photohash<>\"\" and photohash<>\"(null)\";",user.uid];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
        return NULL;
    NSMutableArray *photoHashLists=[[NSMutableArray alloc] init];
    for(int i=0;i<[data count];i++)
    {
        NSDictionary *hashInfo=[data objectAtIndex:i];
        if(hashInfo==NULL||[hashInfo count]==0)
            continue;
        NSString *deviceUid=[hashInfo valueForKey:@"deviceuid"];
        NSString *photoHash=[hashInfo valueForKey:@"photohash"];
        NSMutableDictionary *hashDic=[[NSMutableDictionary alloc] init];
        if(photoHash!=NULL&&![photoHash isEqualToString:@"(null)"]&&![photoHash isEqualToString:@""])
        {
            [hashDic setValue:photoHash forKey:@"photoHash"];
            [hashDic setValue:deviceUid forKey:@"deviceUid"];
        }
        if(hashDic!=NULL&&[hashDic count]>0)
        {
            [photoHashLists addObject:hashDic];
            hashDic=NULL;
        }
        
    }
    return photoHashLists;
}

-(NSArray *)getFailManagerPhotoHash:(NSString *)deviceUid
{
    if([self checkValid]==NO)
        return NULL;
    User *user=[User sharedSingleton];
    if(user==NULL||user.uid==NULL||deviceUid==NULL)
        return NULL;
    NSString *nsql=[NSString stringWithFormat:@"select photohash,user_id from manager where account_id=%@ and (pic_url=null or pic_url=\"(null)\" or pic_url=\"\") and photohash<>\"\" and photohash<>\"(null)\" and deviceuid=%@ and (type=%d or type=%d);",user.uid,deviceUid,ADMIN,VICE_ADMIN];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
        return NULL;
    NSMutableArray *photoHashLists=[[NSMutableArray alloc] init];
    for(int i=0;i<[data count];i++)
    {
        NSDictionary *hashInfo=[data objectAtIndex:i];
        if(hashInfo==NULL||[hashInfo count]==0)
            continue;
        NSString *userId=[hashInfo valueForKey:@"user_id"];
        NSString *photoHash=[hashInfo valueForKey:@"photohash"];
        NSMutableDictionary *hashDic=[[NSMutableDictionary alloc] init];
        if(photoHash!=NULL&&![photoHash isEqualToString:@"(null)"]&&![photoHash isEqualToString:@""])
        {
            [hashDic setValue:photoHash forKey:@"photoHash"];
            [hashDic setValue:userId forKey:@"user_id"];
        }
        if(hashDic!=NULL&&[hashDic count]>0)
        {
            [photoHashLists addObject:hashDic];
            hashDic=NULL;
        }
        
    }
    return photoHashLists;
}

-(NSArray *)getFailContactPhotoHash:(NSString *)deviceUid
{
    if([self checkValid]==NO)
        return NULL;
    User *user=[User sharedSingleton];
    if(user==NULL||user.uid==NULL||deviceUid==NULL)
        return NULL;
    NSString *nsql=[NSString stringWithFormat:@"select photohash,user_id from manager where account_id=%@ and (pic_url=null or pic_url=\"(null)\" or pic_url=\"\") and photohash<>\"\" and photohash<>\"(null)\" and deviceuid=%@ and (type=%d or type=%d);",user.uid,deviceUid,CONTACT,SOS_CONTACT];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
        return NULL;
    NSMutableArray *photoHashLists=[[NSMutableArray alloc] init];
    for(int i=0;i<[data count];i++)
    {
        NSDictionary *hashInfo=[data objectAtIndex:i];
        if(hashInfo==NULL||[hashInfo count]==0)
            continue;
        NSString *userId=[hashInfo valueForKey:@"user_id"];
        NSString *photoHash=[hashInfo valueForKey:@"photohash"];
        NSMutableDictionary *hashDic=[[NSMutableDictionary alloc] init];
        if(photoHash!=NULL&&![photoHash isEqualToString:@"(null)"]&&![photoHash isEqualToString:@""])
        {
            [hashDic setValue:photoHash forKey:@"photoHash"];
            [hashDic setValue:userId forKey:@"user_id"];
        }
        if(hashDic!=NULL&&[hashDic count]>0)
        {
            [photoHashLists addObject:hashDic];
            hashDic=NULL;
        }
        
    }
    return photoHashLists;
}

@end
